Skip to content

Commit 4ad47a8

Browse files
hdlee27seokhee06
authored andcommitted
add capability sample for pH Measurement (#653)
* add capability sample for pH Measurement Signed-off-by: Hyundo Lee <54927573+hdlee27@users.noreply.github.com> * Update data & time in file * Update date in file Co-authored-by: Hyundo Lee <54927573+hdlee27@users.noreply.github.com> Co-authored-by: 정석희/SmartThings개발그룹(MX)/삼성전자 <seokey.jeong@samsung.com>
1 parent 4e5146b commit 4ad47a8

File tree

2 files changed

+168
-0
lines changed

2 files changed

+168
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/* ***************************************************************************
2+
*
3+
* Copyright 2019-2022 Samsung Electronics All Rights Reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing,
12+
* software distributed under the License is distributed on an
13+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14+
* either express or implied. See the License for the specific
15+
* language governing permissions and limitations under the License.
16+
*
17+
****************************************************************************/
18+
19+
#include <string.h>
20+
#include <stdio.h>
21+
#include <stdlib.h>
22+
23+
#include "st_dev.h"
24+
#include "caps_pHMeasurement.h"
25+
26+
static double caps_pHMeasurement_get_pH_value(caps_pHMeasurement_data_t *caps_data)
27+
{
28+
if (!caps_data) {
29+
printf("caps_data is NULL\n");
30+
return caps_helper_pHMeasurement.attr_pH.min - 1;
31+
}
32+
return caps_data->pH_value;
33+
}
34+
35+
static void caps_pHMeasurement_set_pH_value(caps_pHMeasurement_data_t *caps_data, double value)
36+
{
37+
if (!caps_data) {
38+
printf("caps_data is NULL\n");
39+
return;
40+
}
41+
caps_data->pH_value = value;
42+
}
43+
44+
static const char *caps_pHMeasurement_get_pH_unit(caps_pHMeasurement_data_t *caps_data)
45+
{
46+
if (!caps_data) {
47+
printf("caps_data is NULL\n");
48+
return NULL;
49+
}
50+
return caps_data->pH_unit;
51+
}
52+
53+
static void caps_pHMeasurement_set_pH_unit(caps_pHMeasurement_data_t *caps_data, const char *unit)
54+
{
55+
if (!caps_data) {
56+
printf("caps_data is NULL\n");
57+
return;
58+
}
59+
caps_data->pH_unit = (char *)unit;
60+
}
61+
62+
static void caps_pHMeasurement_attr_pH_send(caps_pHMeasurement_data_t *caps_data)
63+
{
64+
int sequence_no = -1;
65+
66+
if (!caps_data || !caps_data->handle) {
67+
printf("fail to get handle\n");
68+
return;
69+
}
70+
71+
ST_CAP_SEND_ATTR_NUMBER(caps_data->handle,
72+
(char *)caps_helper_pHMeasurement.attr_pH.name,
73+
caps_data->pH_value,
74+
caps_data->pH_unit,
75+
NULL,
76+
sequence_no);
77+
78+
if (sequence_no < 0)
79+
printf("fail to send pH value\n");
80+
else
81+
printf("Sequence number return : %d\n", sequence_no);
82+
}
83+
84+
85+
static void caps_pHMeasurement_init_cb(IOT_CAP_HANDLE *handle, void *usr_data)
86+
{
87+
caps_pHMeasurement_data_t *caps_data = usr_data;
88+
if (caps_data && caps_data->init_usr_cb)
89+
caps_data->init_usr_cb(caps_data);
90+
caps_pHMeasurement_attr_pH_send(caps_data);
91+
}
92+
93+
caps_pHMeasurement_data_t *caps_pHMeasurement_initialize(IOT_CTX *ctx, const char *component, void *init_usr_cb, void *usr_data)
94+
{
95+
caps_pHMeasurement_data_t *caps_data = NULL;
96+
97+
caps_data = malloc(sizeof(caps_pHMeasurement_data_t));
98+
if (!caps_data) {
99+
printf("fail to malloc for caps_pHMeasurement_data\n");
100+
return NULL;
101+
}
102+
103+
memset(caps_data, 0, sizeof(caps_pHMeasurement_data_t));
104+
105+
caps_data->init_usr_cb = init_usr_cb;
106+
caps_data->usr_data = usr_data;
107+
108+
caps_data->get_pH_value = caps_pHMeasurement_get_pH_value;
109+
caps_data->set_pH_value = caps_pHMeasurement_set_pH_value;
110+
caps_data->get_pH_unit = caps_pHMeasurement_get_pH_unit;
111+
caps_data->set_pH_unit = caps_pHMeasurement_set_pH_unit;
112+
caps_data->attr_pH_send = caps_pHMeasurement_attr_pH_send;
113+
caps_data->pH_value = 0;
114+
if (ctx) {
115+
caps_data->handle = st_cap_handle_init(ctx, component, caps_helper_pHMeasurement.id, caps_pHMeasurement_init_cb, caps_data);
116+
}
117+
if (!caps_data->handle) {
118+
printf("fail to init pHMeasurement handle\n");
119+
}
120+
121+
return caps_data;
122+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* ***************************************************************************
2+
*
3+
* Copyright 2019-2022 Samsung Electronics All Rights Reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing,
12+
* software distributed under the License is distributed on an
13+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14+
* either express or implied. See the License for the specific
15+
* language governing permissions and limitations under the License.
16+
*
17+
****************************************************************************/
18+
19+
#include "caps/iot_caps_helper_pHMeasurement.h"
20+
21+
#ifdef __cplusplus
22+
extern "C" {
23+
#endif
24+
25+
typedef struct caps_pHMeasurement_data {
26+
IOT_CAP_HANDLE* handle;
27+
void *usr_data;
28+
void *cmd_data;
29+
30+
double pH_value;
31+
char *pH_unit;
32+
33+
double (*get_pH_value)(struct caps_pHMeasurement_data *caps_data);
34+
void (*set_pH_value)(struct caps_pHMeasurement_data *caps_data, double value);
35+
const char *(*get_pH_unit)(struct caps_pHMeasurement_data *caps_data);
36+
void (*set_pH_unit)(struct caps_pHMeasurement_data *caps_data, const char *unit);
37+
void (*attr_pH_send)(struct caps_pHMeasurement_data *caps_data);
38+
39+
void (*init_usr_cb)(struct caps_pHMeasurement_data *caps_data);
40+
} caps_pHMeasurement_data_t;
41+
42+
caps_pHMeasurement_data_t *caps_pHMeasurement_initialize(IOT_CTX *ctx, const char *component, void *init_usr_cb, void *usr_data);
43+
#ifdef __cplusplus
44+
}
45+
#endif
46+

0 commit comments

Comments
 (0)