3 * Copyright 2019 AT&T Intellectual Property
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 #include "Pendulum_asn_codec.h"
21 #include "per_decoder.h"
22 #include "per_encoder.h"
24 #include "OCTET_STRING.h"
26 static int ASN_DEBUG = 0;
28 int pendulum_asn_encode(Pendulum_t *pend, uint8_t **buffer, uint32_t *len)
32 assert(buffer != NULL);
34 encoded = aper_encode_to_new_buffer(&asn_DEF_Pendulum, 0, pend, (void **)buffer);
36 perror("Failed to aper encode\n");
41 //ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_Pendulum, pend);
45 int pendulum_asn_decode(Pendulum_t *pend, const uint8_t *const buffer, const int len)
47 asn_dec_rval_t dec_ret;
49 // THIS IS IMPORTANT, otherwise: Segmentation fault
50 memset(pend, 0, sizeof(*pend));
52 assert(buffer != NULL);
54 dec_ret = aper_decode(NULL, &asn_DEF_Pendulum, (void **)&pend, buffer, len, 0, 0);
55 if (dec_ret.code != RC_OK) {
56 fprintf(stderr, "ERROR: Failed to decode asn1 message\n");
61 xer_fprint(stdout, &asn_DEF_Pendulum, pend);
66 int pendulum_create_asn_msg(uint8_t **buffer, long sequence,
67 double angle, double torque, const char* msg)
69 //Create Pendulum payload struct
72 pend = calloc(1, sizeof(Pendulum_t));
74 perror("calloc() failed");
78 //convert char* to PrintableString_t*
79 PrintableString_t *payload = calloc(1, sizeof(PrintableString_t));
80 payload->buf = (uint8_t *)msg;
81 payload->size = strlen(msg);
83 pend->strval = payload;
84 pend->sequence = &sequence;
86 pend->torque = &torque;
89 xer_fprint(stdout, &asn_DEF_Pendulum, pend);
91 //Encode Pendulum payload struct to asn1 buffer
93 if(pendulum_asn_encode(pend, buffer, &len) < 0)
97 //fprintf(stderr, "len = %d\n", len);
102 long pendulum_get_sequence(const uint8_t *const buffer, const int len)
105 pendulum_asn_decode(&pend, buffer, len);
107 return *(pend.sequence);
110 double pendulum_get_angle(const uint8_t *const buffer, const int len)
113 pendulum_asn_decode(&pend, buffer, len);
115 return *(pend.angle);
118 double pendulum_get_torque(const uint8_t *const buffer, const int len)
121 pendulum_asn_decode(&pend, buffer, len);
123 return *(pend.torque);
126 char* pendulum_get_strval(const uint8_t *const buffer, const int len)
131 pendulum_asn_decode(&pend, buffer, len);
133 str = (char*)pend.strval->buf;
138 void test_pendulum_msg(void)
140 uint8_t *buffer = NULL;
143 len = pendulum_create_asn_msg(&buffer, 0, angle, 0, NULL);
145 double ex_angle = pendulum_get_angle(buffer, len);
146 fprintf(stderr, "Extracted angle = %f\n", ex_angle);
149 void test_pendulum_asn1(void)
151 fprintf(stderr, "test_pendulum_asn1\n");
155 long sequence = 0; /* OPTIONAL */
156 double angle = 1.5; /* OPTIONAL */
157 //double torque = 0.7; /* OPTIONAL */
159 pend = calloc(1, sizeof(Pendulum_t));
161 perror("calloc() failed");
165 pend->sequence = &sequence;
166 pend->angle = ∠
167 //pend->torque = &torque;
169 xer_fprint(stdout, &asn_DEF_Pendulum, pend);
172 uint8_t *buffer = NULL;
174 pendulum_asn_encode(pend, &buffer, &len);
175 fprintf(stderr, "len = %d\n", len);
179 pendulum_asn_decode(&dec_pend, buffer, len);