Add API allowing xAPPs to send alarm messages
[ric-plt/xapp-frame-cpp.git] / test / rmr_em.c
1 // vim: ts=4 sw=4 noet :
2 /*
3 ==================================================================================
4        Copyright (c) 2020 Nokia
5        Copyright (c) 2020 AT&T Intellectual Property.
6
7    Licensed under the Apache License, Version 2.0 (the "License");
8    you may not use this file except in compliance with the License.
9    You may obtain a copy of the License at
10
11        http://www.apache.org/licenses/LICENSE-2.0
12
13    Unless required by applicable law or agreed to in writing, software
14    distributed under the License is distributed on an "AS IS" BASIS,
15    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16    See the License for the specific language governing permissions and
17    limitations under the License.
18 ==================================================================================
19 */
20
21 /*
22         Mnemonic:       rmr_em.c
23         Abstract:       RMR emulation for testing
24
25         Date:           20 March
26         Author:         E. Scott Daniels
27 */
28
29 #include <unistd.h>
30 #include <string.h>
31 #include <malloc.h>
32
33
34 /*
35         CAUTION:  this is a copy of what is in RMR and it is needed as some of the framework
36         functions access the 'public' fields like state and mtype.
37 */
38 typedef struct {
39     int state;                  // state of processing
40     int mtype;                  // message type
41     int len;                    // length of data in the payload (send or received)
42     unsigned char* payload;     // transported data
43     unsigned char* xaction;     // pointer to fixed length transaction id bytes
44     int sub_id;                 // subscription id
45     int     tp_state;           // transport state (errno) valid only if state != RMR_OK, and even then may not be valid
46
47                                 // these things are off limits to the user application
48     void*   tp_buf;             // underlying transport allocated pointer (e.g. nng message)
49     void*   header;             // internal message header (whole buffer: header+payload)
50     unsigned char* id;          // if we need an ID in the message separate from the xaction id
51     int     flags;              // various MFL_ (private) flags as needed
52     int     alloc_len;          // the length of the allocated space (hdr+payload)
53
54     void*   ring;               // ring this buffer should be queued back to
55     int     rts_fd;             // SI fd for return to sender
56
57     int     cookie;             // cookie to detect user misuse of free'd msg
58 } rmr_mbuf_t;
59
60 typedef struct {
61         char meid[32];
62         char src[32];
63 } header_t;
64
65
66 void* rmr_init( char* port, int flags ) {
67         return malloc( sizeof( char ) * 100 );
68 }
69
70 rmr_mbuf_t* rmr_alloc_msg( void* mrc, int payload_len ) {
71         rmr_mbuf_t*     mbuf;
72         char* p;                                // the tp buffer; also the payload
73         header_t* h;
74         int len;
75
76         len = (sizeof( char ) * payload_len) + sizeof( header_t );
77         p = (char *) malloc( len );
78         if( p == NULL ) {
79                 return NULL;
80         }
81         h = (header_t *) p;
82         memset( p, 0, len );
83
84         mbuf = (rmr_mbuf_t *) malloc( sizeof( rmr_mbuf_t ) );
85         if( mbuf == NULL ) {
86                 free( p );
87                 return NULL;
88         }
89         memset( mbuf, 0, sizeof( rmr_mbuf_t ) );
90
91         mbuf->tp_buf = p;
92         mbuf->payload = (char *)p + sizeof( header_t );
93
94
95         mbuf->len = 0;
96         mbuf->alloc_len = payload_len;
97         mbuf->payload = (void *) p;
98         strncpy( h->src, "host:ip", 8 );
99         strncpy( h->meid, "EniMeini", 9 );
100
101         return mbuf;
102 }
103
104 void rmr_free_msg( rmr_mbuf_t* mbuf ) {
105         if( mbuf ) {
106                 if( mbuf->tp_buf ) {
107                         free( mbuf->tp_buf );
108                 }
109                 free( mbuf );
110         }
111 }
112
113 char* rmr_get_meid( rmr_mbuf_t* mbuf, char* m ) {
114         header_t* h;
115
116         if( mbuf != NULL ) {
117                 if( m == NULL ) {
118                         m = (char *) malloc( sizeof( char ) * 32 );     
119                 }
120                 h = (header_t *) mbuf->tp_buf;
121                 memcpy( m, h->meid, 32 );
122         }
123
124         return m;
125 }
126
127 int rmr_payload_size( rmr_mbuf_t* mbuf ) {
128
129         if( mbuf != NULL ) {
130                 return mbuf->alloc_len;
131         }
132
133         return 0;
134 }
135
136 char *rmr_get_src( rmr_mbuf_t* mbuf, char *m ) {
137         header_t*  h;
138
139         if( mbuf != NULL ) {
140                 if( m == NULL ) {
141                         m = (char *) malloc( sizeof( char ) * 32 );     
142                 }
143                 h = (header_t *) mbuf->tp_buf;
144                 memcpy( m, h->src, 32 );
145         }
146
147         return m;
148 }
149
150 int rmr_str2meid( rmr_mbuf_t* mbuf, unsigned char* s ) {
151         header_t*  h;
152
153         if( mbuf != NULL ) {
154                 if( s == NULL ) {
155                         return 1;
156                 }
157
158                 if( strlen( s ) > 31 ) {
159                         return 1;
160                 }
161
162                 h = (header_t *) mbuf->tp_buf;
163                 strncpy( h->meid, s, 32 );
164                 return 0;
165         }
166
167         return 1;
168 }
169
170 rmr_mbuf_t* rmr_send_msg( void* mrc, rmr_mbuf_t* mbuf ) {
171
172         if( mbuf != NULL ) {
173                 mbuf->state = 0;
174         }
175
176         return mbuf;
177 }
178
179 rmr_mbuf_t* rmr_rts_msg( void* mrc, rmr_mbuf_t* mbuf ) {
180
181         if( mbuf != NULL ) {
182                 mbuf->state = 0;
183         }
184
185         return mbuf;
186 }
187
188 rmr_mbuf_t* rmr_realloc_payload( rmr_mbuf_t* mbuf, int payload_len, int copy, int clone ) {             // ensure message is large enough
189         rmr_mbuf_t* nmb;
190         unsigned char* payload;
191
192         if( mbuf == NULL ) {
193                 return NULL;
194         }
195
196         nmb = rmr_alloc_msg( NULL, payload_len );
197         if( copy ) {
198                 memcpy( nmb->payload, mbuf->payload, mbuf->len );
199                 nmb->len = mbuf->len;
200         } else {
201                 nmb->len = 0;
202         }
203         nmb->state = mbuf->state;
204
205         if( ! clone ) {
206                 free( mbuf );
207         }
208         return  nmb;
209 }
210
211 void rmr_close( void*  mrc ) {
212         return;
213 }
214
215 rmr_mbuf_t* rmr_torcv_msg( void* mrc, rmr_mbuf_t* mbuf, int timeout ) {
216         static int max2receive = 500;
217         static int mtype = 0;
218
219         if( mbuf == NULL ) {
220                 mbuf = rmr_alloc_msg( NULL, 2048 );
221         }
222
223         if( max2receive <= 0 ) {
224                 mbuf->state = 12;
225                 mbuf->len = 0;
226                 mbuf->mtype = -1;
227                 mbuf->sub_id = -1;
228         }
229
230         max2receive--;
231
232         mbuf->state = 0;
233         mbuf->len = 80;
234         mbuf->mtype = mtype;
235         mbuf->sub_id = -1;
236
237         mtype++;
238         if( mtype > 100 ) {
239                 mtype = 0;
240         }
241
242         return mbuf;
243 }
244
245 int rmr_ready( void* mrc ) {
246         static int state = 0;
247
248         if( ! state )  {
249                 state = 1;
250                 return 0;
251         }
252
253         return 1;
254 }
255
256 // ----------------------- wormhole dummies ---------------------------------------------
257
258 typedef int rmr_whid_t;
259
260 extern rmr_whid_t rmr_wh_open( void* vctx, char const* target ) {
261         static int whid = 0;
262
263         if( vctx == NULL ) {
264                 return -1;
265         }
266         return whid++;
267 }
268
269 //extern rmr_mbuf_t* rmr_wh_call( void* vctx, rmr_whid_t whid, rmr_mbuf_t* msg, int call_id, int max_wait );
270
271 extern rmr_mbuf_t* rmr_wh_send_msg( void* vctx, rmr_whid_t whid, rmr_mbuf_t* mbuf ) {
272         if( mbuf != NULL ) {
273                 if( whid >= 0 ) {
274                         mbuf->state = 0;
275                 }
276
277                 mbuf->state = 7;
278         }
279
280         return mbuf;
281 }
282
283 extern int rmr_wh_state( void* vctx, rmr_whid_t whid ) {
284         return whid >= 0;
285 }
286
287 extern void rmr_wh_close( void* vctx, int whid ){
288         return;
289 }
290