[Nanopb] Array Sub message - Decode example code

Posted by [하늘이]
2020. 1. 30. 14:28 IT/C, C++
반응형

Decode sample code (Array Sub message sample)


※ 관련글

  • Google Protobuf
  1. [Google Protobuf] For Java in Android Studio - 설치 및 proto 파일 컴파일
  2. [Google Protobuf] For Java in Android Studio - Gen Java 파일 Android Studio 적용
  3. [Google Protobuf] For Java in Android Studio - Basic example code 기본 소스 코드 사용 샘플
  4. [Google Protobuf] For C++ in Linux - Settings C++ 빌드 환경 만들기
  5. [Google Protobuf] For C++ in Linux - Basic example code 기본 소스 코드 사용 샘플
  • Nanopb Basic type value example
  1. [Nanopb for Google protobuf] Basic type proto sample
  2. [Nanopb for Google protobuf] Basic type proto sample - Sample proto file
  3. [Nanopb for Google protobuf] Basic type proto sample - Encode sample code
  4. [Nanopb for Google protobuf] Basic type proto sample - Decode sample code
  • Nanopb String / bytes value example
  1. [Nanopb for Google protobuf] String/Bytes type proto
  2. [Nanopb for Google protobuf] String/Bytes type proto - Sample proto file 
  3. [Nanopb for Google protobuf] String/Bytes type proto - Encode example code 1
  4. [Nanopb for Google protobuf] String/Bytes type proto - Encode example code 2
  5. [Nanopb for Google protobuf] String/Bytes type proto - Decode example code
  • Nanopb basic submsg example
  1. [Nanopb for Google protobuf] Sub message - Example proto file
  2. [Nanopb for Google protobuf] Sub message - Encode example code
  3. [Nanopb for Google protobuf] Sub message - Decode example code
  • Nanopb array submsg value example
  1. [Nanopb for Google protobuf] Array Sub message - Example proto file
  2. [Nanopb for Google protobuf] Array Sub message - Encode example code
  3. [Nanopb for Google protobuf] Array Sub message - Decode example code

* 전달된 데이터 decode를 위한 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Decode test main
void testDecodeSubMsgArr(int encodeSize,  uint8_t* buffer) {
    //Decode 시키기 위한 inStream 생성
    pb_istream_t stream;
 
    /* Construct a pb_istream_t for reading from the buffer */
    stream = pb_istream_from_buffer(buffer, encodeSize);
 
    /* Uses _init_default to just make sure that the macro works. */
    TestSubMsgArr msg = TestSubMsgArr_init_default;
    /* [Optional] Fill with garbage to better detect initialization errors */
    memset(&msg, 0xAAsizeof(msg));
 
    // Decode 시 전달되는 데이터 수집을 위한 내부 struct 변수 선언 및 초기화
    TestMsgST testMsgSt;
    testMsgSt.subMsgCount = 0;
 
    // decode callback 호출 시 전달시킬 내부 struct 변수 입력
    msg.submsg.arg = &testMsgSt;
    // decode callback 등록
    msg.submsg.funcs.decode = &decode_SubMsgArrCB;
 
    //decode api 호출 시 decode callback api 가 호출 된다.
    if (!pb_decode(&stream, TestSubMsgArr_fields, &msg)) {
        Serial.print("testDecodeSubMsgArr Decoding failed: ");
        Serial.println(PB_GET_ERROR(&stream));
        return;
    } else {
        // Print the result after decoding
        Serial.print("testDecodeSubMsgArr Decoding OK cnt:");
        Serial.println(testMsgSt.subMsgCount);
        Serial.print("msg.testInt32:");
        Serial.println(msg.testInt32);
        
        for(int subMsgCnt = 0; subMsgCnt < testMsgSt.subMsgCount; subMsgCnt++){
            Serial.print("testMsgSt.subDoubleValue:");
            Serial.println(testMsgSt.subMsg[subMsgCnt].subDoubleValue);
            Serial.print("testMsgSt.subInt32Value:");
            Serial.println(testMsgSt.subMsg[subMsgCnt].subInt32Value);
        }
    }
}
cs

 


* submsg decoding을 위한 api.

Encode 된 submsg array size 만큼 callback api가 호출된다.

Encode 성송 시 putDataSubMsg 함수로 데이터를 저장한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// submsg array 를 위한 decode api. encode 시 submsg 가 5개 입력된 경우 callback api 도 5번 호출이 됨.
bool decode_SubMsgArrCB(pb_istream_t *stream, const pb_field_t *field, void **arg){
    TestMsgST *testMsgSt = (TestMsgST*)(*arg);
 
//    Serial.println("decode_SubMsgArrCB OK:Start");
 
    // Create proto submsg buffer for decoding api. 
    SubArrMsg subMsg = SubArrMsg_init_default;
    
    // Call pb_decode api with proto submsg buffer
    if (!pb_decode(stream, SubArrMsg_fields ,&subMsg))
    {
        Serial.print("decode_SubMsgArrCB failed:");
        Serial.println(PB_GET_ERROR(stream));
        return false;
    }
    else{
        // Store submsg data to internal struct data when complete decoding.
        putDataSubMsg(*testMsgSt, subMsg.subDoubleValue, subMsg.subInt32Value);
    }
    return true;
}
cs

 

반응형