1 /*--------------------------------------------------------------------------
2   Project:  HorizonSDK
3   File:     rdt_Segment.cpp
4 
5   Copyright 2009 Nintendo.  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   $Date:: 2010-09-14#$
14   $Rev: 25753 $
15   $Author: hiratsu_daisuke $
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 excees 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 attempt to read too large size of 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         // 現状、これらの制御ビットはデータと混ぜずに単独で送っているはずだから。
75         ASSERT(GetDataLength()==0);
76         return GetSeqNumber() + GetDataLength();
77     }
78     else
79     {
80         // データのセグメントの場合。
81         return GetSeqNumber() + GetDataLength() - 1;
82     }
83 }
84 
85 
86 /*!
87     @brief セグメント長を取得(SYNとFINが勘定に入っている)
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