1 /*
2 * Copyright (c) 2024 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #ifndef NRFS_TEMP_H
8 #define NRFS_TEMP_H
9
10 #include <internal/services/nrfs_temp.h>
11
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15
16 /** @brief Temperature service event types. */
17 typedef enum __NRFS_PACKED {
18 NRFS_TEMP_EVT_MEASURE_DONE, /** Temperature measurement done. */
19 NRFS_TEMP_EVT_CHANGE, /** Temperature threshold crossed. */
20 NRFS_TEMP_EVT_REJECT, /** Request rejected. */
21 } nrfs_temp_evt_type_t;
22
23 /** @brief Temperature service event. */
24 typedef struct {
25 nrfs_temp_evt_type_t type; /** Event type. */
26 int32_t raw_temp; /** Raw temperature in a 2's complement signed value representation.
27 * Valid for @p NRFS_TEMP_EVT_MEASURE_DONE and @p NRFS_TEMP_EVT_CHANGE. */
28 } nrfs_temp_evt_t;
29
30 /** @brief Temperature service event handler type. */
31 typedef void (*nrfs_temp_evt_handler_t)(nrfs_temp_evt_t const * p_evt, void * context);
32
33 /**
34 * @brief Function for initializing the Temperature service.
35 *
36 * @param[in] handler Function called as a response to the request.
37 *
38 * @retval NRFS_SUCCESS Service initialized successfully.
39 * @retval NRFS_ERR_INVALID_STATE Service was already initialized.
40 */
41 nrfs_err_t nrfs_temp_init(nrfs_temp_evt_handler_t handler);
42
43 /**
44 * @brief Function for uninitializing the Temperature service.
45 *
46 * @warning Notifications from previous requests are dropped after service uninitialization.
47 */
48 void nrfs_temp_uninit(void);
49
50 /**
51 * @brief Function for requesting a temperature measurement in Temperature service.
52 *
53 * @param[in] p_context Opaque user data that will be passed to registered callback.
54 *
55 * @retval NRFS_SUCCESS Request sent successfully.
56 * @retval NRFS_ERR_INVALID_STATE Service is uninitialized.
57 * @retval NRFS_ERR_IPC Backend returned error during request sending.
58 */
59 nrfs_err_t nrfs_temp_measure_request(void * p_context);
60
61 /**
62 * @brief Function for subscribing to the temperature monitoring in Temperature service.
63 *
64 * @note If subscription is already active, calling this function again
65 * will overwrite the subscription parameters.
66 *
67 * @param[in] measure_rate_ms Maximal acceptable time between subsequent temperature checks.
68 * @param[in] lower_threshold Temperature lower threshold in a raw 2's complement signed format.
69 * @param[in] upper_threshold Temperature upper threshold in a raw 2's complement signed format.
70 * @param[in] p_context Opaque user data that will be passed to registered callback.
71 *
72 * @retval NRFS_SUCCESS Request sent successfully.
73 * @retval NRFS_ERR_INVALID_STATE Service is uninitialized.
74 * @retval NRFS_ERR_IPC Backend returned error during request sending.
75 */
76 nrfs_err_t nrfs_temp_subscribe(uint16_t measure_rate_ms,
77 int32_t lower_threshold,
78 int32_t upper_threshold,
79 void * p_context);
80
81 /**
82 * @brief Function for unsubscribing from the temperature monitoring in Temperature service.
83 *
84 * @retval NRFS_SUCCESS Request sent successfully.
85 * @retval NRFS_ERR_INVALID_STATE Service is uninitialized.
86 * @retval NRFS_ERR_IPC Backend returned error during request sending.
87 */
88 nrfs_err_t nrfs_temp_unsubscribe(void);
89
90 /**
91 * @brief Function for converting the temperature value from raw data to Celsius scale.
92 *
93 * The returned temperature value is in Celsius scale, multiplied by 100.
94 * For example, the actual temperature of 25.75[C] will be returned as a 2575 signed integer.
95 * Measurement accuracy is 0.25[C].
96 *
97 * @param[in] raw_temp Temperature value in a 2's complement signed value representation.
98 *
99 * @return Temperature in Celsius scale.
100 */
nrfs_temp_from_raw(int32_t raw_temp)101 static inline int32_t nrfs_temp_from_raw(int32_t raw_temp)
102 {
103 /* Raw temperature is a 2's complement signed value. Moreover, it is represented
104 * by 0.25[C] intervals, so division by 4 is needed. To preserve
105 * fractional part, raw value is multiplied by 100 before division.*/
106
107 return (raw_temp * 100) / 4;
108 }
109
110 /**
111 * @brief Function for converting a temperature value from Celsius scale to raw data.
112 *
113 * The returned temperature value is a 2's complement signed value representation.
114 * For example, actual temperature of 25.75[C] should be provided as 2575 signed integer,
115 * and will be returned as a 103 integer.
116 * During conversion, the accuracy will be reduced to 0.25[C] steps.
117 *
118 * @param[in] temp_c Temperature value in Celsius scale, multiplied by 100.
119 *
120 * @return Temperature in RAW format.
121 */
nrfs_temp_to_raw(int32_t temp_c)122 static inline int32_t nrfs_temp_to_raw(int32_t temp_c)
123 {
124 return (temp_c * 4) / 100;
125 }
126
127 #ifdef __cplusplus
128 }
129 #endif
130
131 #endif /* NRFS_TEMP_H */
132