Add unit tests and changes related
[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         return rmr_alloc_msg( NULL, payload_len );
190 }
191
192 void rmr_close( void*  mrc ) {
193         return;
194 }
195
196 rmr_mbuf_t* rmr_torcv_msg( void* mrc, rmr_mbuf_t* mbuf, int timeout ) {
197         static int max2receive = 500;
198         static int mtype = 0;
199
200         if( mbuf == NULL ) {
201                 mbuf = rmr_alloc_msg( NULL, 2048 );
202         }
203
204         if( max2receive <= 0 ) {
205                 mbuf->state = 12;
206                 mbuf->len = 0;
207                 mbuf->mtype = -1;
208                 mbuf->sub_id = -1;
209         }
210
211         max2receive--;
212
213         mbuf->state = 0;
214         mbuf->len = 80;
215         mbuf->mtype = mtype;
216         mbuf->sub_id = -1;
217
218         mtype++;
219         if( mtype > 100 ) {
220                 mtype = 0;
221         }
222
223         return mbuf;
224 }
225
226 int rmr_ready( void* mrc ) {
227         static int state = 0;
228
229         if( ! state )  {
230                 state = 1;
231                 return 0;
232         } 
233
234         return 1;
235 }
236