1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     rdt_Segment.cpp
4 
5   Copyright (C)2009-2012 Nintendo Co., Ltd.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Rev: 46347 $
14  *---------------------------------------------------------------------------*/
15 
16 #include "stdafx.h"
17 
18 #include "rdt_Segment.h"
19 
20 #include <string.h>
21 
22 #include "rdt_Utility.h"
23 
24 namespace
25 {
26 
27 }  // End of anonymous namespace
28 
29 namespace nn { namespace rdt { namespace CTR {
30 
31 
32 
SetData(const void * pBuf,u32 len)33 void Segment::SetData(const void *pBuf, u32 len)
34 {
35     ASSERT(pBuf!=NULL);
36     ASSERTMSG(len <= PAYLOAD_SIZE, "Amount of data exceeds payload.\n");;
37 
38     memcpy(payload, pBuf, len);
39     header.dataLength = len;
40 }
41 
42 
GetData(void * pBuf,u32 len) const43 u32 Segment::GetData(void *pBuf, u32 len) const
44 {
45     ASSERT(pBuf!=NULL);
46     WARNINGMSG((len <= PAYLOAD_SIZE), "You attempted to read too much data (bigger than payload size).  Ignored.");
47 
48     u32 ret = min(header.dataLength, len);
49     memcpy(pBuf, payload, ret);
50 
51     return ret;
52 }
53 
54 
Clear(void)55 void Segment::Clear(void)
56 {
57     header.Clear();
58     memset(payload, 0, PAYLOAD_SIZE);
59 }
60 
61 
ClearHeader(void)62 void Segment::ClearHeader(void)
63 {
64     header.Clear();
65 }
66 
67 
GetLastSeqNumber(void) const68 u32 Segment::GetLastSeqNumber(void) const
69 {
70     if(IsSyn() || IsAck() || IsFin() || IsRst())
71     {
72         // Because these control bits should only be sent separately and not mixed with data.
73         ASSERT(GetDataLength()==0);
74         return GetSeqNumber() + GetDataLength();
75     }
76     else
77     {
78         // For data segments.
79         return GetSeqNumber() + GetDataLength() - 1;
80     }
81 }
82 
83 
84 /* Please see man pages for details
85 
86 */
GetSegmentLength(void) const87 u32 Segment::GetSegmentLength(void) const
88 {
89     u32 ret = header.dataLength;
90     if(IsSyn())
91     {
92         ++ret;
93     }
94     if(IsFin())
95     {
96         ++ret;
97     }
98 
99     return ret;
100 }
101 
102 
PrintDebugInfo(void) const103 void Segment::PrintDebugInfo(void) const
104 {
105     LOG("--Header contents--\n");
106     header.PrintDebugInfo();
107     if(GetDataLength()==0)
108     {
109         LOG("--No data--\n");
110     }
111     else
112     {
113         char buf[PAYLOAD_SIZE+1];
114         u32 len = GetData(buf, PAYLOAD_SIZE);
115         buf[len] = '\0';
116         LOG("--Data(%d bytes)--\n", GetDataLength());
117 //        LOG("%s\n", buf);
118     }
119 }
120 
121 
122 
123 }}} // namespace nn::rdt::CTR
124