[Nanopb] Array Sub message - Example proto file

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

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

Sample proto file (Array Sub message sample)

syntax = "proto2";


message SubArrMsg {

    required int32 subInt32Value = 1;

    required double subDoubleValue = 2;

}


message TestSubMsgArr {

    required int32 testInt32 = 1;

    repeated SubArrMsg submsg = 2;

}



* proto file에 main struct 내에 sub struct 배열이 존재하는 경우 처리를 하는 경우 필요한 코드 설명이다.

 

* submsg 를 전달하고 저장하기 위하여 struct 타입을 하나 만들어서 사용한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#define MAX_SUB_MSG_CNT 10
/* Struct internal definitions */
typedef struct {
    int32_t subInt32Value;
    double subDoubleValue;
} TestSubMsgST;
 
//샘플 설명을 위하여 protofile 의 submsg만 대응. max값등은 맞춰서 사용하면 됨.
typedef struct {
    int32_t subMsgCount;
    TestSubMsgST subMsg[MAX_SUB_MSG_CNT];
} TestMsgST;
 
// 위 구조체 값을 저장시키기 위한 공톹 api 를 만들어서 사용함. add a data to struct
void putDataSubMsg(TestMsgST &testData, double doubleValue, int intValue){
    testData.subMsg[testData.subMsgCount].subDoubleValue = doubleValue;
    testData.subMsg[testData.subMsgCount].subInt32Value = intValue;
    testData.subMsgCount++;
}
 
cs

* Test Result

TestSubMsgArr encode OK size:48

SubMsgArr Bytes: 08 C1 02 12 07 08 1E 11 B6 33 CA 42 12 07 08 1F 11 B6 33 CC 42 12 07 08 20 11 B6 33 CE 42 12 07 08 21 11 B6 33 D0 42 12 07 08 22 11 B6 33 D2 42 

testDecodeSubMsgArr Decoding OK cnt:5

msg.testInt32:321

testMsgSt.subDoubleValue:101.10

testMsgSt.subInt32Value:30

testMsgSt.subDoubleValue:102.10

testMsgSt.subInt32Value:31

testMsgSt.subDoubleValue:103.10

testMsgSt.subInt32Value:32

testMsgSt.subDoubleValue:104.10

testMsgSt.subInt32Value:33

testMsgSt.subDoubleValue:105.10

testMsgSt.subInt32Value:34


반응형