Front Haul Interface Library first seed code contribution
[o-du/phy.git] / fhi_lib / app / common / common.c
1 /******************************************************************************
2 *
3 *   Copyright (c) 2019 Intel.
4 *
5 *   Licensed under the Apache License, Version 2.0 (the "License");
6 *   you may not use this file except in compliance with the License.
7 *   You may obtain a copy of the License at
8 *
9 *       http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *   Unless required by applicable law or agreed to in writing, software
12 *   distributed under the License is distributed on an "AS IS" BASIS,
13 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *   See the License for the specific language governing permissions and
15 *   limitations under the License.
16 *
17 *******************************************************************************/
18
19 #ifndef _XRAN_APP_COMMON_
20 #define _XRAN_APP_COMMON_
21
22 #include <assert.h>
23 #include <err.h>
24 #include <arpa/inet.h>
25 #include <sys/time.h>
26 #include <time.h>
27
28 #include "../common/common.h"
29 #include "xran_pkt.h"
30 #include "xran_pkt_up.h"
31 #include "xran_cp_api.h"
32 #include "xran_up_api.h"
33 #include "../src/xran_printf.h"
34
35
36 #define MBUFS_CNT 256
37
38 extern enum app_state state;
39
40 uint8_t numCCPorts = 1;
41 /* Number of antennas supported by front-end */
42
43 uint8_t num_eAxc = 4;
44 /* Number of CPRI ports supported by front-end */
45
46 int16_t *p_tx_play_buffer[MAX_ANT_CARRIER_SUPPORTED];
47 int32_t tx_play_buffer_size[MAX_ANT_CARRIER_SUPPORTED];
48 int32_t tx_play_buffer_position[MAX_ANT_CARRIER_SUPPORTED];
49
50 int16_t *p_rx_log_buffer[MAX_ANT_CARRIER_SUPPORTED];
51 int32_t rx_log_buffer_size[MAX_ANT_CARRIER_SUPPORTED];
52 int32_t rx_log_buffer_position[MAX_ANT_CARRIER_SUPPORTED];
53
54 int16_t *p_prach_log_buffer[MAX_ANT_CARRIER_SUPPORTED];
55 int32_t prach_log_buffer_size[MAX_ANT_CARRIER_SUPPORTED];
56 int32_t prach_log_buffer_position[MAX_ANT_CARRIER_SUPPORTED];
57
58 int16_t *p_tx_buffer[MAX_ANT_CARRIER_SUPPORTED];
59 int32_t tx_buffer_size[MAX_ANT_CARRIER_SUPPORTED];
60
61 int16_t *p_rx_buffer[MAX_ANT_CARRIER_SUPPORTED];
62 int32_t rx_buffer_size[MAX_ANT_CARRIER_SUPPORTED];
63
64 void sys_save_buf_to_file(char *filename, char *bufname, unsigned char *pBuffer, unsigned int size, unsigned int buffers_num)
65 {
66     if (size)
67     {
68         if (filename && bufname)
69         {
70             FILE           *file;
71             printf("Storing %s to file %s: ", bufname, filename);
72             file = fopen(filename, "wb");
73             if (file == NULL)
74             {
75                 print_err("can't open file %s!!!", filename);
76             }
77             else
78             {
79                 uint32_t             num;
80                 num = fwrite(pBuffer, buffers_num, size, file);
81                 fflush(file);
82                 fclose(file);
83                 printf("from addr (0x%lx) size (%d) bytes num (%d)", (uint64_t)pBuffer, size, num);
84             }
85             printf(" \n");
86         }
87         else
88         {
89             print_err(" the file name, buffer name are not set!!!");
90         }
91     }
92     else
93     {
94         print_err(" the %s is free: size = %d bytes!!!", bufname, size);
95     }
96 }
97
98 int sys_load_file_to_buff(char *filename, char *bufname, unsigned char *pBuffer, unsigned int size, unsigned int buffers_num)
99 {
100     unsigned int  file_size = 0;
101     int  num= 0;
102
103     if (size)
104     {
105         if (filename && bufname)
106         {
107             FILE           *file;
108             printf("Loading file %s to  %s: ", filename, bufname);
109             file = fopen(filename, "rb");
110
111
112             if (file == NULL)
113             {
114                 print_err("can't open file %s!!!", filename);
115                 exit(-1);
116             }
117             else
118             {
119                 fseek(file, 0, SEEK_END);
120                 file_size = ftell(file);
121                 fseek(file, 0, SEEK_SET);
122
123                 if ((file_size > size) || (file_size == 0))
124                     file_size = size;
125
126                 printf("Reading IQ samples from file: File Size: %d [Buffer Size: %d]\n", file_size, size);
127
128                 num = fread(pBuffer, buffers_num, size, file);
129                 fflush(file);
130                 fclose(file);
131                 printf("from addr (0x%lx) size (%d) bytes num (%d)", (uint64_t)pBuffer, file_size, num);
132             }
133             printf(" \n");
134
135         }
136         else
137         {
138             print_err(" the file name, buffer name are not set!!!");
139         }
140     }
141     else
142     {
143         print_err(" the %s is free: size = %d bytes!!!", bufname, size);
144     }
145     return num;
146 }
147
148
149 void sys_save_buf_to_file_txt(char *filename, char *bufname, unsigned char *pBuffer, unsigned int size, unsigned int buffers_num)
150 {
151     unsigned int i;
152     int ret = 0;
153     if (pBuffer == NULL)
154         return;
155
156     if (size)
157     {
158         if (filename && bufname)
159         {
160             FILE           *file;
161             printf("Storing %s to file %s: ", bufname, filename);
162             file = fopen(filename, "w");
163             if (file == NULL)
164             {
165                 print_err("can't open file %s!!!", filename);
166                 exit(-1);
167             }
168             else
169             {
170                 uint32_t num = 0;
171
172                 signed short *ptr = (signed short*)pBuffer;
173                 for (i = 0; i < (size/((unsigned int)sizeof(signed short) /** 2 * 2 * 2*/)); i = i + 2)
174                 {
175                     ret = fprintf(file,"%d %d\n", ptr[i], ptr[i + 1]);
176                     if (ret < 0)
177                     {
178                         printf("fprintf %d\n", ret);
179                         fclose(file);
180                         break;
181                     }
182                     num++;
183                 }
184                 fflush(file);
185                 fclose(file);
186                 printf("from addr (0x%lx) size (%d) IQ num (%d)", (uint64_t)pBuffer, size, num);
187             }
188             printf(" \n");
189         }
190         else
191         {
192             print_err(" the file name, buffer name are not set!!!");
193         }
194     }
195     else
196     {
197         print_err(" the %s is free: size = %d bytes!!!", bufname, size);
198     }
199 }
200
201
202 #endif /* _XRAN_APP_COMMON_ */