sphin_sensor_surface.c (5906B)
1 /* Copyright (C) 2024-2026 Centre National de la Recherche Scientifique 2 * Copyright (C) 2024-2026 Clermont Auvergne INP 3 * Copyright (C) 2024-2026 INSA Lyon 4 * Copyright (C) 2024-2026 Institut Mines Télécom Albi-Carmaux 5 * Copyright (C) 2024-2026 Institut National Polytechnique de Toulouse 6 * Copyright (C) 2024-2026 |Méso|Star> (contact@meso-star.com) 7 * Copyright (C) 2024-2026 PhotonLyX (info@photonlyx.com) 8 * Copyright (C) 2024-2026 Université de Lorraine 9 * Copyright (C) 2024-2026 Université Paul Sabatier 10 * Copyright (C) 2024-2026 Université Toulouse - Jean Jaurès 11 * 12 * This program is free software: you can redistribute it and/or modify 13 * it under the terms of the GNU General Public License as published by 14 * the Free Software Foundation, either version 3 of the License, or 15 * (at your option) any later version. 16 * 17 * This program is distributed in the hope that it will be useful, 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 * GNU General Public License for more details. 21 * 22 * You should have received a copy of the GNU General Public License 23 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ 24 #define _POSIX_C_SOURCE 200112L /* for strtok_r support */ 25 26 #include "sphin.h" 27 #include "sphin_c.h" 28 #include "sphin_config.h" 29 #include "sphin_sensor.h" 30 #include "sphin_sensor_surface.h" 31 32 #include <rsys/mem_allocator.h> 33 #include <rsys/ref_count.h> 34 #include <rsys/rsys.h> 35 #include <rsys/str.h> 36 #include <rsys/text_reader.h> 37 38 struct txtrdr; 39 40 struct sphin_sensor_surface { 41 struct str name; 42 double response_function; 43 44 struct sphin* sphin; 45 ref_T ref; 46 }; 47 48 /******************************************************************************* 49 * Helper functions 50 ******************************************************************************/ 51 static res_T 52 sensor_surface_create 53 (struct sphin* sphin, 54 const char* name, 55 struct sphin_sensor_surface** out_sensor_surface) 56 { 57 struct sphin_sensor_surface* sensor_surface = NULL; 58 res_T res = RES_OK; 59 60 ASSERT(NULL != sphin); 61 ASSERT(NULL != out_sensor_surface); 62 ASSERT(NULL != name); 63 ASSERT('\0' != name[0]); /* Name can't be empty */ 64 65 sensor_surface = MEM_CALLOC 66 (sphin->allocator, 1, sizeof(struct sphin_sensor_surface)); 67 if (NULL == sensor_surface) { res = RES_MEM_ERR; goto error; } 68 ref_init(&sensor_surface->ref); 69 SPHIN(ref_get(sphin)); 70 sensor_surface->sphin = sphin; 71 72 str_init(sphin->allocator, &sensor_surface->name); 73 res = str_set(&sensor_surface->name, name); 74 if (RES_OK != res) { goto error; } 75 76 exit: 77 *out_sensor_surface = sensor_surface; 78 return res; 79 error: 80 if (NULL != sensor_surface) { 81 SPHIN(sensor_surface_ref_put(sensor_surface)); 82 sensor_surface = NULL; 83 } 84 goto exit; 85 } 86 87 static void 88 release_sensor_surface 89 (ref_T* address) 90 { 91 struct sphin_sensor_surface* sensor_surface = NULL; 92 struct sphin* sphin = NULL; 93 94 ASSERT(NULL != address); 95 sensor_surface = CONTAINER_OF(address, struct sphin_sensor_surface, ref); 96 str_release(&sensor_surface->name); 97 sphin = sensor_surface->sphin; 98 MEM_RM(sphin->allocator, sensor_surface); 99 SPHIN(ref_put(sphin)); 100 } 101 102 /******************************************************************************* 103 * Local functions 104 ******************************************************************************/ 105 res_T 106 parse_sensor_surface 107 (struct sphin* sphin, 108 struct txtrdr* txtrdr, 109 const char* name, 110 struct sphin_sensor_surface** out_sensor_surface) 111 { 112 char* keyword = NULL; 113 char* token = NULL; 114 char* token_ptr = NULL; 115 char* value = NULL; 116 struct sphin_sensor_surface* sensor_surface = NULL; 117 struct str line; 118 res_T res = RES_OK; 119 120 ASSERT(NULL != sphin); 121 ASSERT(NULL != txtrdr); 122 ASSERT(NULL != name); 123 ASSERT(NULL != out_sensor_surface); 124 125 str_init(sphin->allocator, &line); 126 127 res = sensor_surface_create(sphin, name, &sensor_surface); 128 if (RES_OK != res) { goto error; } 129 130 res = txtrdr_read_line(txtrdr); 131 if (RES_OK != res) { goto error; } 132 133 while (NULL != txtrdr_get_line(txtrdr)) { 134 res = str_set(&line, txtrdr_get_cline(txtrdr)); 135 if (RES_OK != res) { goto error; } 136 137 /* Parse keyword */ 138 token = strtok_r(str_get(&line), ":", &token_ptr); 139 if (NULL == token){ res = RES_BAD_ARG; goto error; } 140 keyword = trim_keyword(token); 141 if (NULL == keyword){ res = RES_BAD_ARG; goto error; } 142 143 /* Parse value */ 144 token = strtok_r(NULL, "", &token_ptr); 145 value = token; 146 /* Parse response function */ 147 if (0 == strcmp(keyword, "response_function")){ 148 res = parse_response_function(&sensor_surface->response_function, 149 txtrdr, value); 150 } 151 else { 152 break; 153 } 154 if (RES_OK != res) { goto error; } 155 } 156 157 exit: 158 str_release(&line); 159 *out_sensor_surface = sensor_surface; 160 return res; 161 error: 162 if (sensor_surface != NULL){ 163 SPHIN(sensor_surface_ref_put(sensor_surface)); 164 sensor_surface = NULL; 165 } 166 goto exit; 167 } 168 169 /******************************************************************************* 170 * Exported functions 171 ******************************************************************************/ 172 res_T 173 sphin_sensor_surface_ref_get 174 (struct sphin_sensor_surface* sensor_surface) 175 { 176 if (NULL == sensor_surface) { 177 return RES_BAD_ARG; 178 } 179 ref_get(&sensor_surface->ref); 180 return RES_OK; 181 } 182 183 res_T 184 sphin_sensor_surface_ref_put 185 (struct sphin_sensor_surface* sensor_surface) 186 { 187 if (NULL == sensor_surface) { 188 return RES_BAD_ARG; 189 } 190 ref_put(&sensor_surface->ref, release_sensor_surface); 191 return RES_OK; 192 } 193 194 res_T 195 sphin_sensor_surface_get_response_function 196 (struct sphin_sensor_surface* sensor_surface, 197 double* response_function) 198 { 199 if (NULL == sensor_surface || NULL == response_function){ 200 return RES_BAD_ARG; 201 } 202 203 *response_function = sensor_surface->response_function; 204 return RES_OK; 205 }