找回密码
 立即注册
查看: 5673|回复: 2

Tizen网络音频样本概述

[复制链接]
发表于 2017-11-2 22:48:15 | 显示全部楼层 |阅读模式
网络音频示例应用程序演示了用户如何管理事物。 源代码显示了在初始化,启动,所有权转移和资源处理的情况下使用st-things SDK的工作。

先决条件
为确保正确的应用程序执行,必须设置以下权限:

http://tizen.org/privilege/network.get
http://tizen.org/privilege/network.set
http://tizen.org/privilege/internet
http://tizen.org/privilege/alarm.set
http://tizen.org/privilege/network.profile

必须安装Tizen Studio 2.0。
在包管理器中,必须安装主SDK中的4.0 Mobile。
在软件包管理器中,必须安装扩展SDK中的其他> IOT-Headless-4.0
必须安装IoT安装向导(Tizen Studio插件)。
设备必须通过IoT安装向导上的SD卡闪烁,并且必须安装连接驱动程序。
您必须准备证书(* .pem)和私钥(* .der)。
有关与IoT安装向导相关的上述步骤,请参阅IoT安装向导


Implementation您可以使用Tizen Studio自定义iot-headless v4.0模板开发示例应用程序。 但是,以下示例使用导入的项目中的示例应用程序代码。 您可以在Tizen Studio中下载此示例应用程序。

以下内容在thing.c文件中:

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdbool.h>
  4. #include <app_common.h>
  5. #include "st_things.h"
  6. #include "user.h"
  7. #include "headlessthingsapp.h"

  8. #define JSON_PATH "device_def.json"

  9. static const char *URI_POWERSWITCH = "/switch/main/0";
  10. static const char *URI_VOLUME = "/audioVolume/main/0";

  11. /* Handle: for getting request on resources */
  12. static bool
  13. handle_get_request(st_things_get_request_message_s *req_msg, st_things_representation_s *resp_rep)
  14. {
  15.     DBG("resource_uri [%s]", req_msg->resource_uri);

  16.     if (0 == strcmp(req_msg->resource_uri, URI_POWERSWITCH))
  17.         return handle_get_request_on_switch(req_msg, resp_rep);
  18.     if (0 == strcmp(req_msg->resource_uri, URI_VOLUME))
  19.         return handle_get_request_on_volume(req_msg, resp_rep);

  20.     ERR("not supported uri");

  21.     return false;
  22. }

  23. /* Handle: for setting request on resources */
  24. static bool
  25. handle_set_request(st_things_set_request_message_s *req_msg, st_things_representation_s *resp_rep)
  26. {
  27.     DBG("resource_uri [%s]", req_msg->resource_uri);

  28.     if (0 == strcmp(req_msg->resource_uri, URI_POWERSWITCH))
  29.         return handle_set_request_on_switch(req_msg, resp_rep);
  30.     if (0 == strcmp(req_msg->resource_uri, URI_VOLUME))
  31.         return handle_set_request_on_volume(req_msg, resp_rep);

  32.     ERR("not supported uri");

  33.     return false;
  34. }

  35. /* Initialize */
  36. void
  37. init_thing()
  38. {
  39.     FN_CALL;
  40.     static bool binitialized = false;
  41.     if (binitialized) {
  42.         DBG("Already initialized!!");

  43.         return;
  44.     }
  45.     binitialized = true;
  46.     init_user();

  47.     bool easysetup_complete = false;

  48.     char app_json_path[128] = {0,};
  49.     char *app_res_path = app_get_resource_path();
  50.     snprintf(app_json_path, sizeof(app_json_path), "%s/%s", app_res_path, JSON_PATH);
  51.     free(app_res_path);

  52.     if (0 != st_things_initialize(app_json_path, &easysetup_complete)) {
  53.         ERR("st_things_initialize() failed!!");

  54.         return;
  55.     }

  56.     DBG("easysetup_complete:[%d]", easysetup_complete);

  57.     st_things_register_request_cb(handle_get_request, handle_set_request);
  58.     st_things_register_reset_cb(handle_reset_request, handle_reset_result);
  59.     st_things_register_user_confirm_cb(handle_ownership_transfer_request);
  60.     st_things_register_things_status_change_cb(handle_things_status_change);

  61.     st_things_start();

  62.     FN_END;
  63. }
复制代码
以下内容在user.h和user.c文件中:
  1. #ifndef __USER_H__
  2. #define  __USER_H__

  3. #include <stdio.h>
  4. #include <string.h>

  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif

  8. bool handle_get_request_on_switch(st_things_get_request_message_s* req_msg, st_things_representation_s* resp_rep);
  9. bool handle_set_request_on_switch(st_things_set_request_message_s* req_msg, st_things_representation_s* resp_rep);
  10. bool handle_get_request_on_volume(st_things_get_request_message_s* req_msg, st_things_representation_s* resp_rep);
  11. bool handle_set_request_on_volume(st_things_set_request_message_s* req_msg, st_things_representation_s* resp_rep);

  12. bool handle_reset_request();
  13. void handle_reset_result(bool result);
  14. bool handle_ownership_transfer_request();
  15. void handle_things_status_change(st_things_status_e things_status);
  16. bool init_user();

  17. #ifdef __cplusplus
  18. }
  19. #endif

  20. #endif /* __USER_H__ */

  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <stdbool.h>
  24. #include <inttypes.h>
  25. #include <app_common.h>
  26. #include "st_things.h"
  27. #include "user.h"
  28. #include "player.h"
  29. #include "headlessthingsapp.h"

  30. #define VALUE_STR_LEN_MAX 32

  31. static const char* KEY_SWITCH = "power";
  32. static const char* VALUE_SWITCH_ON = "on";
  33. static const char* VALUE_SWITCH_OFF = "off";
  34. static char g_switch[VALUE_STR_LEN_MAX+1] = "off";

  35. static const char* KEY_VOLUME = "volume";
  36. static int64_t g_volume;

  37. static const char* KEY_MUTE = "mute";
  38. static bool g_mute;

  39. static const char* KEY_VOLUME_COMMAND = "command";
  40. static const char* VALUE_VOLUME_COMMAND_INC = "increase";
  41. static const char* VALUE_VOLUME_COMMAND_DEC = "decrease";
  42. static const char* VALUE_VOLUME_COMMAND_MAX = "max";
  43. static const char* VALUE_VOLUME_COMMAND_MIN = "min";
  44. static char g_volume_cmd[VALUE_STR_LEN_MAX+1] = "increase";

  45. /******************************************************/

  46. #define AUDIO_FILE_NAME "sample.mp3"

  47. static player_h g_player;

  48. static char audio_file[128];

  49. /* Player: get mute */
  50. static bool
  51. user_player_is_muted(bool *muted)
  52. {
  53.     FN_CALL;

  54.     int ret;
  55.     ret = player_is_muted(g_player, muted);
  56.     if (ret != PLAYER_ERROR_NONE) {
  57.         ERR("player_is_muted is failed [%d]", ret);

  58.         return false;
  59.     }

  60.     return true;
  61. }

  62. /* Player: set mute */
  63. static bool
  64. user_player_set_mute(bool muted)
  65. {
  66.     FN_CALL;

  67.     int ret;
  68.     ret = player_set_mute(g_player, muted);
  69.     if (ret != PLAYER_ERROR_NONE) {
  70.         ERR("player_set_mute is failed [%d]", ret);

  71.         return false;
  72.     }

  73.     return true;
  74. }

  75. /* Player: set volume */
  76. static bool
  77. user_player_set_volume(float vol)
  78. {
  79.     FN_CALL;

  80.     int ret;
  81.     ret = player_set_volume(g_player, vol, vol);
  82.     if (ret != PLAYER_ERROR_NONE) {
  83.         ERR("player_set_volume is failed [%d]", ret);

  84.         return false;
  85.     }

  86.     return true;
  87. }

  88. /* Player: get volume */
  89. static bool
  90. user_player_get_volume(float *vol)
  91. {
  92.     FN_CALL;

  93.     int ret;
  94.     float vol2;
  95.     ret = player_get_volume(g_player, vol, &vol2);
  96.     if (ret != PLAYER_ERROR_NONE) {
  97.         ERR("player_get_volume is failed [%d]", ret);

  98.         return false;
  99.     }

  100.     return true;
  101. }

  102. /* Player: start player */
  103. static bool
  104. user_player_start()
  105. {
  106.     FN_CALL;

  107.     int ret;
  108.     ret = player_set_uri(g_player, audio_file);
  109.     if (ret != PLAYER_ERROR_NONE) {
  110.         ERR("player_set_uri is failed [%d]", ret);

  111.         return false;
  112.     }
  113.     ret = player_prepare(g_player);
  114.     if (ret != PLAYER_ERROR_NONE) {
  115.         ERR("player_prepare is failed [%d]", ret);

  116.         return false;
  117.     }
  118.     ret = player_start(g_player);
  119.     if (ret != PLAYER_ERROR_NONE) {
  120.         ERR("player_start is failed [%d]", ret);

  121.         return false;
  122.     }

  123.     return true;
  124. }

  125. /* Player: stop player */
  126. static bool
  127. user_player_stop()
  128. {
  129.     FN_CALL;

  130.     int ret;
  131.     ret = player_stop(g_player);
  132.     if (ret != PLAYER_ERROR_NONE) {
  133.         ERR("player_stop is failed [%d]", ret);

  134.         return false;
  135.     }
  136.     ret = player_unprepare(g_player);
  137.     if (ret != PLAYER_ERROR_NONE) {
  138.         ERR("player_unprepare is failed [%d]", ret);

  139.         return false;
  140.     }

  141.     return true;
  142. }

  143. /* Player: init player */
  144. static bool
  145. user_player_init()
  146. {
  147.     FN_CALL;

  148.     int ret;
  149.     ret = player_create(&g_player);
  150.     if (ret != PLAYER_ERROR_NONE) {
  151.         ERR("player_create is failed [%d]", ret);

  152.         return false;
  153.     }
  154.     ret = player_set_looping(g_player, true);
  155.     if (ret != PLAYER_ERROR_NONE) {
  156.         ERR("player_set_looping is failed [%d]", ret);

  157.         return false;
  158.     }

  159.     char *app_res_path = app_get_resource_path();
  160.     snprintf(audio_file, sizeof(audio_file), "%s/%s", app_res_path, AUDIO_FILE_NAME);
  161.     free(app_res_path);

  162.     return true;
  163. }

  164. /* Initialize player */
  165. bool
  166. init_user()
  167. {
  168.     FN_CALL;

  169.     int ret;

  170.     ret = user_player_init();

  171.     return ret;
  172. }

  173. /* Handle: for getting request on switch */
  174. bool
  175. handle_get_request_on_switch(st_things_get_request_message_s *req_msg, st_things_representation_s *resp_rep)
  176. {
  177.     DBG("current g_switch: [%s]", g_switch);
  178.     resp_rep->set_str_value(resp_rep, KEY_SWITCH, g_switch);

  179.     return true;
  180. }

  181. /* Handle: for setting request on switch */
  182. bool
  183. handle_set_request_on_switch(st_things_set_request_message_s *req_msg, st_things_representation_s *resp_rep)
  184. {
  185.     DBG("current g_switch: [%s]", g_switch);

  186.     char *str_value = NULL;
  187.     req_msg->rep->get_str_value(req_msg->rep, KEY_SWITCH, &str_value);
  188.     DBG("requested switch: [%s]", str_value);

  189.     /* Check validation */
  190.     if ((0 != strncmp(str_value, VALUE_SWITCH_ON, strlen(VALUE_SWITCH_ON)))
  191.         && (0 != strncmp(str_value, VALUE_SWITCH_OFF, strlen(VALUE_SWITCH_OFF)))) {
  192.         ERR("Not supported value!!");
  193.         free(str_value);

  194.         return false;
  195.     }

  196.     if (0 != strncmp(str_value, g_switch, strlen(g_switch))) {
  197.         strncpy(g_switch, str_value, VALUE_STR_LEN_MAX);
  198.         if (0 == strncmp(g_switch, VALUE_SWITCH_ON, strlen(VALUE_SWITCH_ON)))
  199.             user_player_start();
  200.         else
  201.             user_player_stop();
  202.     }
  203.     resp_rep->set_str_value(resp_rep, KEY_SWITCH, g_switch);

  204.     st_things_notify_observers(req_msg->resource_uri);

  205.     free(str_value);

  206.     return true;
  207. }

  208. /* Handle: for getting request on volume */
  209. bool
  210. handle_get_request_on_volume(st_things_get_request_message_s *req_msg, st_things_representation_s *resp_rep)
  211. {
  212.     DBG("current g_volume: [%lld], g_mute:[%d]", g_volume, g_mute);

  213.     if (req_msg->has_property_key(req_msg, KEY_VOLUME)) {
  214.         DBG("key[%s] exists", KEY_VOLUME);
  215.         float temp_vol = 0.0;
  216.         user_player_get_volume(&temp_vol);
  217.         g_volume = (int64_t) (temp_vol * 100);
  218.         DBG("current g_volume: [%lld]", g_volume);
  219.         resp_rep->set_int_value(resp_rep, KEY_VOLUME, g_volume);
  220.     }

  221.     if (req_msg->has_property_key(req_msg, KEY_MUTE)) {
  222.         DBG("key[%s] exists", KEY_MUTE);
  223.         user_player_is_muted(&g_mute);
  224.         DBG("current g_mute: [%d]", g_mute);
  225.         resp_rep->set_bool_value(resp_rep, KEY_MUTE, g_mute);
  226.     }

  227.     if (req_msg->has_property_key(req_msg, KEY_VOLUME_COMMAND))
  228.         WARN("key[%s] exists..Ignore It!", KEY_VOLUME_COMMAND);

  229.     return true;
  230. }

  231. /* Handle: for setting request on volume */
  232. bool
  233. handle_set_request_on_volume(st_things_set_request_message_s *req_msg, st_things_representation_s *resp_rep)
  234. {
  235.     DBG("current g_volume: [%lld], g_mute:[%d]", g_volume, g_mute);

  236.     int64_t ivalue;
  237.     if (req_msg->rep->get_int_value(req_msg->rep, KEY_VOLUME, &ivalue)) {
  238.         DBG("requested volume: [%lld]", ivalue);
  239.         g_volume = ivalue;
  240.         float temp_vol = (float) (g_volume/100.);
  241.         user_player_set_volume(temp_vol);
  242.         resp_rep->set_int_value(resp_rep, KEY_VOLUME, g_volume);
  243.     }

  244.     bool bvalue;
  245.     if (req_msg->rep->get_bool_value(req_msg->rep, KEY_MUTE, &bvalue)) {
  246.         DBG("requested mute: [%d]", bvalue);
  247.         g_mute = bvalue;
  248.         user_player_set_mute(g_mute);
  249.         resp_rep->set_bool_value(resp_rep, KEY_MUTE, g_mute);
  250.     }

  251.     char *str_value = NULL;
  252.     if (req_msg->rep->get_str_value(req_msg->rep, KEY_VOLUME_COMMAND, &str_value)) {
  253.         DBG("requested volume_cmd: [%s]", str_value);

  254.         strncpy(g_volume_cmd, str_value, VALUE_STR_LEN_MAX);
  255.         if (0 == strncmp(g_volume_cmd, VALUE_VOLUME_COMMAND_INC, strlen(VALUE_VOLUME_COMMAND_INC)))
  256.             DBG("TODO");
  257.         else if (0 == strncmp(g_volume_cmd, VALUE_VOLUME_COMMAND_DEC, strlen(VALUE_VOLUME_COMMAND_DEC)))
  258.             DBG("TODO");
  259.         else if (0 == strncmp(g_volume_cmd, VALUE_VOLUME_COMMAND_MAX, strlen(VALUE_VOLUME_COMMAND_MAX)))
  260.             DBG("TODO");
  261.         else if (0 == strncmp(g_volume_cmd, VALUE_VOLUME_COMMAND_MIN, strlen(VALUE_VOLUME_COMMAND_MIN)))
  262.             DBG("TODO");
  263.         else
  264.             ERR("Not supported command");

  265.         resp_rep->set_str_value(resp_rep, KEY_VOLUME_COMMAND, g_volume_cmd);
  266.         free(str_value);
  267.     }

  268.     st_things_notify_observers(req_msg->resource_uri);

  269.     return true;
  270. }

  271. /* Handle: reset request*/
  272. bool
  273. handle_reset_request()
  274. {
  275.     bool confirmed = true;
  276.     DBG("confirmed : [%d]", confirmed);

  277.     return confirmed;
  278. }

  279. /* Handle: reset result */
  280. void
  281. handle_reset_result(bool result)
  282. {
  283.     DBG("result : [%d]", result);
  284. }

  285. /* Handle: ownership transfer request */
  286. bool
  287. handle_ownership_transfer_request()
  288. {
  289.     bool confirmed = true;
  290.     DBG("confirmed : [%d]", confirmed);

  291.     return confirmed;
  292. }

  293. /* Handle: for things status change */
  294. void
  295. handle_things_status_change(st_things_status_e things_status)
  296. {
  297.     DBG("things_status : [%d]", things_status);
  298. }
复制代码
测试
测试样品

1.将您的ARTIK 530或Raspberry Pi 3板通过USB连接到Linux计算机,然后打开SDB。
2.右键单击项目,然后选择“运行方式”>“Tizen Native项目”。


3.要检查样品是否启动,请使用SDB shell中的ifconfig命令检查Wi-Fi(Soft AP)是否已打开。
使用您的Samsung Connect App搜索并添加设备。 然后,您可以看到目标板已连接到SmartThings Cloud,Samsung Connect App在其UI中显示此设备。

附:
PREV Tizen外设I / O Native API


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
欢迎来到泰泽网:http://www.tizennet.com/ 泰泽论坛:http://bbs.tizennet.com/ 好没有内涵哦,快到设置中更改这个无聊的签名吧!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|泰泽应用|泰泽论坛|泰泽网|小黑屋|Archiver|手机版|泰泽邮箱|泰泽网 ( 蜀ICP备13024062号-1 )

GMT+8, 2024-11-24 17:02 , Processed in 0.051183 second(s), 24 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表