1 /*
2  * Copyright 2018 Oticon A/S
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #ifndef BS_PCOM_BASE_H
7 #define BS_PCOM_BASE_H
8 
9 #include <stddef.h>
10 #include <stdbool.h>
11 #include <unistd.h>
12 #include "bs_types.h"
13 #include "bs_pc_base_types.h"
14 
15 #ifdef __cplusplus
16 extern "C"{
17 #endif
18 
19 int pb_create_fifo_if_not_there(const char* fifo_name);
20 int pb_create_com_folder(const char* s);
21 void pb_send_payload(int ff, void *buf, size_t size);
22 BSIM_INLINE void pb_send_msg(int ff, pc_header_t header,
23                              void *s, size_t s_size);
24 
25 typedef struct {
26   char **ff_path_dtp;
27   char **ff_path_ptd;
28   int *ff_dtp;
29   int *ff_ptd;
30   unsigned int n_devices;
31   bool *device_connected;
32   char *lock_path;
33 } pb_phy_state_t;
34 
35 int pb_phy_is_connected_to_device(pb_phy_state_t * this, uint d);
36 int pb_phy_initcom(pb_phy_state_t *this, const char* s, const char *p, uint n);
37 void pb_phy_disconnect_devices(pb_phy_state_t *this);
38 pc_header_t pb_phy_get_next_request(pb_phy_state_t *this, uint d);
39 void pb_phy_get_wait_s(pb_phy_state_t *this, uint d, pb_wait_t *wait_s);
40 void pb_phy_resp_wait(pb_phy_state_t *this, uint d);
41 void pb_phy_free_one_device(pb_phy_state_t *this, int d);
42 
43 typedef struct {
44   int ff_dtp;
45   int ff_ptd;
46   char *ff_path_dtp;
47   char *ff_path_ptd;
48   bool connected;
49   unsigned int this_dev_nbr;
50   char *lock_path;
51 } pb_dev_state_t;
52 
53 int pb_dev_init_com(pb_dev_state_t *this, uint d, const char* s, const char *p);
54 void pb_dev_disconnect(pb_dev_state_t *this);
55 void pb_dev_terminate(pb_dev_state_t *this);
56 void pb_dev_clean_up(pb_dev_state_t *this);
57 int pb_dev_read(pb_dev_state_t *this, void *buf, size_t n_bytes);
58 int pb_dev_request_wait_block(pb_dev_state_t *this, pb_wait_t *wait_s);
59 int pb_dev_request_wait_nonblock(pb_dev_state_t *this, pb_wait_t *wait_s);
60 int pb_dev_pick_wait_resp(pb_dev_state_t *this);
61 
pb_send_msg(int ff,pc_header_t header,void * s,size_t s_size)62 BSIM_INLINE void pb_send_msg(int ff, pc_header_t header,
63                              void *s, size_t s_size) {
64 #pragma GCC diagnostic push
65 #pragma GCC diagnostic ignored "-Wunused-result"
66     write(ff, &header, sizeof(header));
67     write(ff, s, s_size);
68 #pragma GCC diagnostic pop
69 }
70 
71 #define CHECK_CONNECTED(c) \
72   if (!c){ \
73     bs_trace_error_line("Programming error: Not connected\n"); \
74     return -1; \
75   }
76 
77 #define COM_FAILED_ERROR "Low level communication with phy failed"
78 
79 #define INVALID_RESP(header) \
80     bs_trace_error_line(COM_FAILED_ERROR \
81                         ": Received invalid response %i\n", header)
82 
83 #ifdef __cplusplus
84 }
85 #endif
86 
87 #endif
88