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