873c2c1a8e6b7162b7d586c5fd23a3cac54c0260
[o-du/phy.git] / fhi_lib / lib / src / xran_sync_api.c
1 /******************************************************************************\r
2 *\r
3 *   Copyright (c) 2019 Intel.\r
4 *\r
5 *   Licensed under the Apache License, Version 2.0 (the "License");\r
6 *   you may not use this file except in compliance with the License.\r
7 *   You may obtain a copy of the License at\r
8 *\r
9 *       http://www.apache.org/licenses/LICENSE-2.0\r
10 *\r
11 *   Unless required by applicable law or agreed to in writing, software\r
12 *   distributed under the License is distributed on an "AS IS" BASIS,\r
13 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14 *   See the License for the specific language governing permissions and\r
15 *   limitations under the License.\r
16 *\r
17 *******************************************************************************/\r
18 /**\r
19  * @brief This file provides implementation of synchronization related APIs (PTP/1588)\r
20  *        for XRAN.\r
21  *\r
22  * @file xran_sync_api.c\r
23  * @ingroup group_lte_source_xran\r
24  * @author Intel Corporation\r
25  *\r
26  **/\r
27 \r
28 #include <assert.h>\r
29 #include <dirent.h>\r
30 #include <stdio.h>\r
31 #include <stdlib.h>\r
32 #include <string.h>\r
33 \r
34 #include "xran_sync_api.h"\r
35 #include "xran_printf.h"\r
36 \r
37 #define BUF_LEN 256\r
38 #define PROC_DIR "/proc"\r
39 #define COMM_FILE "comm"\r
40 #define PMC_CMD "pmc -u -b 0 'GET PORT_DATA_SET'"\r
41 #define PTP4L_PROC_NAME "ptp4l"\r
42 #define PHC2SYS_PROC_NAME "phc2sys"\r
43 \r
44 static int find_substr(const char *str, const unsigned int str_len,\r
45     const char *substr, const unsigned int substr_len)\r
46 {\r
47     assert(str);\r
48     assert(substr);\r
49     unsigned int ind = 0;\r
50 \r
51     while (ind + substr_len <= str_len) {\r
52         if (0 == strncmp(&str[ind], substr, substr_len))\r
53             return 0;\r
54         ++ind;\r
55     }\r
56     return 1;\r
57 }\r
58 \r
59 static int is_process_running(char *pname)\r
60 {\r
61     char full_path[BUF_LEN] = {0};\r
62     char read_proc_name[BUF_LEN] = {0};\r
63     int res = 1;\r
64     DIR *dir = opendir(PROC_DIR);\r
65     if (NULL == dir) {\r
66         return 1;\r
67     }\r
68 \r
69     struct dirent *entry = NULL;\r
70     while (entry = readdir(dir)) {\r
71         long pid = atol(entry->d_name);\r
72         if (0 == pid)\r
73             continue;\r
74         snprintf(full_path, sizeof(full_path), "%s/%ld/%s", PROC_DIR, pid, COMM_FILE);\r
75         FILE *proc_name_file = fopen(full_path, "r");\r
76         if (NULL == proc_name_file)\r
77             continue;\r
78         fgets( read_proc_name, BUF_LEN, proc_name_file);\r
79         if (0 == strncmp(read_proc_name, pname, strlen(pname))) {\r
80             res = 0;\r
81             fclose(proc_name_file);\r
82             break;\r
83         }\r
84         fclose(proc_name_file);\r
85     }\r
86     closedir(dir);\r
87     return res;\r
88 }\r
89 \r
90 static int check_ptp_status()\r
91 {\r
92     char pmc_out_line[BUF_LEN];\r
93     const char *keywords[2] = {"portState", "SLAVE"};\r
94     int res = 1;\r
95     FILE *pmc_pipe = popen(PMC_CMD, "r");\r
96     if (NULL == pmc_pipe)\r
97         return 1;\r
98 \r
99     while(fgets(pmc_out_line, BUF_LEN, pmc_pipe)) {\r
100         if (0 == find_substr(pmc_out_line, strlen(pmc_out_line), keywords[0],\r
101             strlen(keywords[0]))) {\r
102             if (0 == find_substr(pmc_out_line, strlen(pmc_out_line),\r
103                 keywords[1], strlen(keywords[1]))) {\r
104                 res = 0;\r
105                 break;\r
106             }\r
107         }\r
108     }\r
109     fclose(pmc_pipe);\r
110     return res;\r
111 }\r
112 \r
113 int xran_is_synchronized()\r
114 {\r
115     int res = 0;\r
116     res |= is_process_running(PTP4L_PROC_NAME);\r
117     print_dbg("PTP4L_PROC_NAME %d\n", res);\r
118     res |= is_process_running(PHC2SYS_PROC_NAME);\r
119     print_dbg("PHC2SYS_PROC_NAME %d\n", res);\r
120     res |= check_ptp_status();\r
121     print_dbg("check_ptp_status %d\n", res);\r
122     return res;\r
123 }\r