1 /*
2  *    Copyright (c) 2017, The OpenThread Authors.
3  *    All rights reserved.
4  *
5  *    Redistribution and use in source and binary forms, with or without
6  *    modification, are permitted provided that the following conditions are met:
7  *    1. Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *    2. Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *    3. Neither the name of the copyright holder nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  *    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  *    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  *    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20  *    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  *    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  *    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  *    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  *    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  *    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 /**
29  * @file
30  *   This file implements a spinel encoder.
31  */
32 
33 #include "spinel_encoder.hpp"
34 
35 #include <string.h>
36 
37 #include "common/code_utils.hpp"
38 
39 namespace ot {
40 namespace Spinel {
41 
BeginFrame(Spinel::Buffer::Priority aPriority)42 otError Encoder::BeginFrame(Spinel::Buffer::Priority aPriority)
43 {
44     mNumOpenStructs = 0;
45     mNcpBuffer.InFrameBegin(aPriority);
46     return OT_ERROR_NONE;
47 }
48 
BeginFrame(uint8_t aHeader,unsigned int aCommand)49 otError Encoder::BeginFrame(uint8_t aHeader, unsigned int aCommand)
50 {
51     otError error = OT_ERROR_NONE;
52 
53     // Non-zero TID indicates this is a response to a spinel command.
54 
55     if (SPINEL_HEADER_GET_TID(aHeader) != 0)
56     {
57         SuccessOrExit(error = BeginFrame(Spinel::Buffer::kPriorityHigh));
58     }
59     else
60     {
61         SuccessOrExit(error = BeginFrame(Spinel::Buffer::kPriorityLow));
62     }
63 
64     SuccessOrExit(error = WriteUint8(aHeader));
65     SuccessOrExit(error = WriteUintPacked(aCommand));
66 
67 exit:
68     return error;
69 }
70 
BeginFrame(uint8_t aHeader,unsigned int aCommand,spinel_prop_key_t aKey)71 otError Encoder::BeginFrame(uint8_t aHeader, unsigned int aCommand, spinel_prop_key_t aKey)
72 {
73     otError error = OT_ERROR_NONE;
74 
75     SuccessOrExit(error = BeginFrame(aHeader, aCommand));
76 
77     // The write position is saved before writing the property key,
78     // so that if fetching the property fails and we need to
79     // reply with a `LAST_STATUS` error we can get back to
80     // this saved write position and update the property key.
81     // (Also see `OverwriteWithLastStatusError()`).
82 
83     SuccessOrExit(error = SavePosition());
84     SuccessOrExit(error = WriteUintPacked(aKey));
85 
86 exit:
87     return error;
88 }
89 
OverwriteWithLastStatusError(spinel_status_t aStatus)90 otError Encoder::OverwriteWithLastStatusError(spinel_status_t aStatus)
91 {
92     otError error = OT_ERROR_NONE;
93 
94     SuccessOrExit(error = ResetToSaved());
95     SuccessOrExit(error = WriteUintPacked(SPINEL_PROP_LAST_STATUS));
96     SuccessOrExit(error = WriteUintPacked(aStatus));
97 
98 exit:
99     return error;
100 }
101 
EndFrame(void)102 otError Encoder::EndFrame(void)
103 {
104     otError error = OT_ERROR_NONE;
105 
106     while (mNumOpenStructs > 0)
107     {
108         SuccessOrExit(error = CloseStruct());
109     }
110 
111     error = mNcpBuffer.InFrameEnd();
112 
113 exit:
114     return error;
115 }
116 
WriteUint16(uint16_t aUint16)117 otError Encoder::WriteUint16(uint16_t aUint16)
118 {
119     otError error = OT_ERROR_NONE;
120 
121     SuccessOrExit(error = mNcpBuffer.InFrameFeedByte((aUint16 >> 0) & 0xff));
122     SuccessOrExit(error = mNcpBuffer.InFrameFeedByte((aUint16 >> 8) & 0xff));
123 
124 exit:
125     return error;
126 }
127 
WriteUint32(uint32_t aUint32)128 otError Encoder::WriteUint32(uint32_t aUint32)
129 {
130     otError error = OT_ERROR_NONE;
131 
132     SuccessOrExit(error = mNcpBuffer.InFrameFeedByte((aUint32 >> 0) & 0xff));
133     SuccessOrExit(error = mNcpBuffer.InFrameFeedByte((aUint32 >> 8) & 0xff));
134     SuccessOrExit(error = mNcpBuffer.InFrameFeedByte((aUint32 >> 16) & 0xff));
135     SuccessOrExit(error = mNcpBuffer.InFrameFeedByte((aUint32 >> 24) & 0xff));
136 
137 exit:
138     return error;
139 }
140 
WriteUint64(uint64_t aUint64)141 otError Encoder::WriteUint64(uint64_t aUint64)
142 {
143     otError error = OT_ERROR_NONE;
144 
145     SuccessOrExit(error = mNcpBuffer.InFrameFeedByte((aUint64 >> 0) & 0xff));
146     SuccessOrExit(error = mNcpBuffer.InFrameFeedByte((aUint64 >> 8) & 0xff));
147     SuccessOrExit(error = mNcpBuffer.InFrameFeedByte((aUint64 >> 16) & 0xff));
148     SuccessOrExit(error = mNcpBuffer.InFrameFeedByte((aUint64 >> 24) & 0xff));
149     SuccessOrExit(error = mNcpBuffer.InFrameFeedByte((aUint64 >> 32) & 0xff));
150     SuccessOrExit(error = mNcpBuffer.InFrameFeedByte((aUint64 >> 40) & 0xff));
151     SuccessOrExit(error = mNcpBuffer.InFrameFeedByte((aUint64 >> 48) & 0xff));
152     SuccessOrExit(error = mNcpBuffer.InFrameFeedByte((aUint64 >> 56) & 0xff));
153 
154 exit:
155     return error;
156 }
157 
WriteUintPacked(unsigned int aUint)158 otError Encoder::WriteUintPacked(unsigned int aUint)
159 {
160     uint8_t        buffer[6];
161     spinel_ssize_t len;
162 
163     len = spinel_packed_uint_encode(buffer, sizeof(buffer), aUint);
164 
165     return WriteData(buffer, static_cast<uint16_t>(len));
166 }
167 
WriteDataWithLen(const uint8_t * aData,uint16_t aDataLen)168 otError Encoder::WriteDataWithLen(const uint8_t *aData, uint16_t aDataLen)
169 {
170     otError error = OT_ERROR_NONE;
171 
172     SuccessOrExit(error = WriteUint16(aDataLen));
173     SuccessOrExit(error = WriteData(aData, aDataLen));
174 
175 exit:
176     return error;
177 }
178 
WriteUtf8(const char * aUtf8)179 otError Encoder::WriteUtf8(const char *aUtf8)
180 {
181     otError error;
182     size_t  len = strlen(aUtf8);
183 
184     if (len >= 0xffff)
185     {
186         len = 0xffff;
187     }
188 
189     SuccessOrExit(error = WriteData(reinterpret_cast<const uint8_t *>(aUtf8), static_cast<uint16_t>(len)));
190     SuccessOrExit(error = mNcpBuffer.InFrameFeedByte(0));
191 
192 exit:
193     return error;
194 }
195 
OpenStruct(void)196 otError Encoder::OpenStruct(void)
197 {
198     otError error = OT_ERROR_NONE;
199 
200     VerifyOrExit(mNumOpenStructs < kMaxNestedStructs, error = OT_ERROR_INVALID_STATE);
201     SuccessOrExit(error = mNcpBuffer.InFrameGetPosition(mStructPosition[mNumOpenStructs]));
202 
203     // Reserve bytes for the length to be filled when the struct gets closed.
204     SuccessOrExit(error = mNcpBuffer.InFrameFeedByte(0));
205     SuccessOrExit(error = mNcpBuffer.InFrameFeedByte(0));
206 
207     mNumOpenStructs++;
208 
209 exit:
210     return error;
211 }
212 
CloseStruct(void)213 otError Encoder::CloseStruct(void)
214 {
215     otError  error = OT_ERROR_NONE;
216     uint16_t len;
217     uint8_t  buffer[sizeof(uint16_t)];
218 
219     VerifyOrExit(mNumOpenStructs > 0, error = OT_ERROR_INVALID_STATE);
220 
221     mNumOpenStructs--;
222 
223     len = mNcpBuffer.InFrameGetDistance(mStructPosition[mNumOpenStructs]);
224     VerifyOrExit(len >= sizeof(uint16_t), error = OT_ERROR_INVALID_STATE);
225 
226     len -= sizeof(uint16_t);
227 
228     buffer[0] = (len >> 0 & 0xff);
229     buffer[1] = (len >> 8 & 0xff);
230 
231     SuccessOrExit(error = mNcpBuffer.InFrameOverwrite(mStructPosition[mNumOpenStructs], buffer, sizeof(buffer)));
232 
233 exit:
234     return error;
235 }
236 
SavePosition(void)237 otError Encoder::SavePosition(void)
238 {
239     otError error = OT_ERROR_NONE;
240 
241     SuccessOrExit(error = mNcpBuffer.InFrameGetPosition(mSavedPosition));
242     mSavedNumOpenStructs = mNumOpenStructs;
243 
244 exit:
245     return error;
246 }
247 
ResetToSaved(void)248 otError Encoder::ResetToSaved(void)
249 {
250     otError error = OT_ERROR_NONE;
251 
252     SuccessOrExit(error = mNcpBuffer.InFrameReset(mSavedPosition));
253     mNumOpenStructs = mSavedNumOpenStructs;
254 
255 exit:
256     return error;
257 }
258 
WritePacked(const char * aPackFormat,...)259 otError Encoder::WritePacked(const char *aPackFormat, ...)
260 {
261     uint8_t        buf[kPackFormatBufferSize];
262     otError        error = OT_ERROR_NONE;
263     spinel_ssize_t packedLen;
264     va_list        args;
265 
266     va_start(args, aPackFormat);
267 
268     packedLen = spinel_datatype_vpack(buf, sizeof(buf), aPackFormat, args);
269     VerifyOrExit((packedLen > 0) && (packedLen <= static_cast<spinel_ssize_t>(sizeof(buf))), error = OT_ERROR_NO_BUFS);
270 
271     error = mNcpBuffer.InFrameFeedData(buf, static_cast<uint16_t>(packedLen));
272 
273 exit:
274     va_end(args);
275 
276     return error;
277 }
278 
WriteVPacked(const char * aPackFormat,va_list aArgs)279 otError Encoder::WriteVPacked(const char *aPackFormat, va_list aArgs)
280 {
281     uint8_t        buf[kPackFormatBufferSize];
282     otError        error = OT_ERROR_NONE;
283     spinel_ssize_t packedLen;
284 
285     packedLen = spinel_datatype_vpack(buf, sizeof(buf), aPackFormat, aArgs);
286     VerifyOrExit((packedLen > 0) && (packedLen <= static_cast<spinel_ssize_t>(sizeof(buf))), error = OT_ERROR_NO_BUFS);
287 
288     error = mNcpBuffer.InFrameFeedData(buf, static_cast<uint16_t>(packedLen));
289 
290 exit:
291     return error;
292 }
293 
294 } // namespace Spinel
295 } // namespace ot
296