Enhanced SIM for E2AP v1 for TS UC
[sim/e2-interface.git] / e2sim / e2apv1sim / test / Pendulum / Pendulum_asn_codec.c
1 /*
2  *
3  * Copyright 2019 AT&T Intellectual Property
4  * Copyright 2019 Nokia
5  *
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19
20 #include "Pendulum_asn_codec.h"
21 #include "per_decoder.h"
22 #include "per_encoder.h"
23
24 #include "OCTET_STRING.h"
25
26 static int ASN_DEBUG = 0;
27
28 int pendulum_asn_encode(Pendulum_t *pend, uint8_t **buffer, uint32_t *len)
29 {
30   ssize_t encoded;
31   assert(pend != NULL);
32   assert(buffer != NULL);
33
34   encoded = aper_encode_to_new_buffer(&asn_DEF_Pendulum, 0, pend, (void **)buffer);
35   if(encoded < 0){
36     perror("Failed to aper encode\n");
37     exit(1);
38   }
39
40   *len = encoded;
41   //ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_Pendulum, pend);
42   return encoded;
43 }
44
45 int pendulum_asn_decode(Pendulum_t *pend, const uint8_t *const buffer, const int len)
46 {
47   asn_dec_rval_t dec_ret;
48
49   // THIS IS IMPORTANT, otherwise: Segmentation fault
50   memset(pend, 0, sizeof(*pend));
51
52   assert(buffer != NULL);
53
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");
57     return -1;
58   }
59
60   if(ASN_DEBUG)
61     xer_fprint(stdout, &asn_DEF_Pendulum, pend);
62
63   return 0;
64 }
65
66 int pendulum_create_asn_msg(uint8_t **buffer, long sequence,
67   double angle, double torque, const char* msg)
68 {
69   //Create Pendulum payload struct
70   Pendulum_t *pend;
71
72   pend = calloc(1, sizeof(Pendulum_t));
73   if(!pend){
74     perror("calloc() failed");
75     exit(1);
76   }
77
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);
82
83   pend->strval    = payload;
84   pend->sequence  = &sequence;
85   pend->angle     = &angle;
86   pend->torque    = &torque;
87
88   if (ASN_DEBUG)
89     xer_fprint(stdout, &asn_DEF_Pendulum, pend);
90
91   //Encode Pendulum payload struct to asn1 buffer
92   uint32_t  len;
93   if(pendulum_asn_encode(pend, buffer, &len) < 0)
94   {
95     return -1;
96   }
97   //fprintf(stderr, "len = %d\n", len);
98
99   return len;
100 }
101
102 long pendulum_get_sequence(const uint8_t *const buffer, const int len)
103 {
104   Pendulum_t pend;
105   pendulum_asn_decode(&pend, buffer, len);
106
107   return *(pend.sequence);
108 }
109
110 double pendulum_get_angle(const uint8_t *const buffer, const int len)
111 {
112   Pendulum_t pend;
113   pendulum_asn_decode(&pend, buffer, len);
114
115   return *(pend.angle);
116 }
117
118 double pendulum_get_torque(const uint8_t *const buffer, const int len)
119 {
120   Pendulum_t pend;
121   pendulum_asn_decode(&pend, buffer, len);
122
123   return *(pend.torque);
124 }
125
126 char* pendulum_get_strval(const uint8_t *const buffer, const int len)
127 {
128   Pendulum_t pend;
129   char* str;
130
131   pendulum_asn_decode(&pend, buffer, len);
132
133   str = (char*)pend.strval->buf;
134
135   return str;
136 }
137
138 void test_pendulum_msg(void)
139 {
140   uint8_t   *buffer = NULL;
141   uint32_t  len = 0;
142   double    angle = 1.9;
143   len = pendulum_create_asn_msg(&buffer, 0, angle, 0, NULL);
144
145   double ex_angle = pendulum_get_angle(buffer, len);
146   fprintf(stderr, "Extracted angle = %f\n", ex_angle);
147 }
148
149 void test_pendulum_asn1(void)
150 {
151   fprintf(stderr, "test_pendulum_asn1\n");
152
153   Pendulum_t *pend;
154
155   long  sequence = 0;   /* OPTIONAL */
156   double        angle = 1.5;    /* OPTIONAL */
157   //double      torque = 0.7;   /* OPTIONAL */
158
159   pend = calloc(1, sizeof(Pendulum_t));
160   if(!pend){
161     perror("calloc() failed");
162     exit(1);
163   }
164
165   pend->sequence  = &sequence;
166   pend->angle     = &angle;
167   //pend->torque    = &torque;
168
169   xer_fprint(stdout, &asn_DEF_Pendulum, pend);
170
171   //encode
172   uint8_t   *buffer = NULL;
173   uint32_t  len;
174   pendulum_asn_encode(pend, &buffer, &len);
175   fprintf(stderr, "len = %d\n", len);
176
177   //decode
178   Pendulum_t dec_pend;
179   pendulum_asn_decode(&dec_pend, buffer, len);
180 }