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_test_and_create_lock_file(const char *filename);
54 void pb_remove_lock_file(char** file_path);
55 int pb_device_test_and_create_lock_file(pb_dev_state_t *this, const char *phy_id, unsigned int dev_nbr);
56
57 int pb_dev_init_com(pb_dev_state_t *this, uint d, const char* s, const char *p);
58 void pb_dev_disconnect(pb_dev_state_t *this);
59 void pb_dev_terminate(pb_dev_state_t *this);
60 void pb_dev_clean_up(pb_dev_state_t *this);
61 int pb_dev_read(pb_dev_state_t *this, void *buf, size_t n_bytes);
62 int pb_dev_request_wait_block(pb_dev_state_t *this, pb_wait_t *wait_s);
63 int pb_dev_request_wait_nonblock(pb_dev_state_t *this, pb_wait_t *wait_s);
64 int pb_dev_pick_wait_resp(pb_dev_state_t *this);
65
pb_send_msg(int ff,pc_header_t header,void * s,size_t s_size)66 BSIM_INLINE void pb_send_msg(int ff, pc_header_t header,
67 void *s, size_t s_size) {
68 #pragma GCC diagnostic push
69 #pragma GCC diagnostic ignored "-Wunused-result"
70 write(ff, &header, sizeof(header));
71 write(ff, s, s_size);
72 #pragma GCC diagnostic pop
73 }
74
75 #define CHECK_CONNECTED(c) \
76 if (!c){ \
77 bs_trace_error_line("Programming error: Not connected\n"); \
78 return -1; \
79 }
80
81 #define COM_FAILED_ERROR "Low level communication with phy failed"
82
83 #define INVALID_RESP(header) \
84 bs_trace_error_line(COM_FAILED_ERROR \
85 ": Received invalid response %i\n", header)
86
87 #ifdef __cplusplus
88 }
89 #endif
90
91 #endif
92