# RTOS equipment

For RTOS devices, we introduce a cloud-to-cloud solution, that is, a cloud proxy solution.

Cloud-to-Cloud scheme is designed for low-power devices that cannot run a complete VoIP SDK. It connects the WeChat VoIP server to the WeChat low-power device through the voice video data flow and signaling in the device vendor's background.

The call process:

SDK underload address; https://git.weixin.qq.com/wxa_iot/cloudvoipsdk

# 1. Equipment requirements

  • HTTPS communication capability
  • Storage capacity
  • Audio (visual) capabilities

# 2. Device-side development

An integrated device-side SDK is required on low-power devices to make VoIP calls to Weixin Mini Program within WeChat of the phone.

The device SDK is initialized with Weixin Mini Program AppID, model id, device id, data file path call wx_init.

If the device is running the device SDK for the first time, you also need to call the wx_device_register function to register the current device with the WeChat background. The snticket of Weixin Mini Program is required to register the device.

If the registration is successful, then you can call the wx_cloudvoip_client_call function to initiate a WeChat voip call.The OpenID of the WeChat user who is going to call the device and the audio and video cloud backend recognize the payload required for the VoIP (content is decided by the vendor and the cloud server backend and will be transmitted to the cloud server background). If the call is successful, Note that the device is allowed to make audio and video calls to the specified WeChat user, the vendor can then create the audio and video data flow to the vendor's cloud server and start the transmission (this step can also be initiated before the call function). In short, the payload is the identification provided to the manufacturer's device to its cloud server channel, which facilitates the cloud server to find the audio and video stream transmitted by the device after receiving the WeChat background notification.

Note that the WeChat user must first authorize the permission to initiate calls on the specified device in the manufacturer's Weixin Mini Program,If you do not close the authorization or delete the Mini Program (clean up all data and authorization information from the Mini Program) in the Mini Program settings, the device will not have the right to initiate the call.

# 3. Interface Dxplaination

The specific interface is as follows, please refer to the demo code in the example directory of the sdk package.

/**
 * @brief 初始化 voip
 *
 * 调用其它接口之前需要调用 wx_init() 初始化
 *
 *
 * @param stack (nonnull) 开发者实现 TLS 连接、关闭、读、写接口。
 * @param hal (nonnull) 开发者实现 OS 相关的接口
 * @param config (nonnull) 设备配置信息
 * @return
 *  - WXERROR_RESOURCE_EXHAUSTED: 内存资源分配失败
 *  - WXERROR_INVALID_DEVICEID: 设备已注册,但当前 sdk 用的 deviceid、modelid 不对
 */
wx_error_t wx_init(wxvoip_network_https_impl_t *stack, wxvoip_os_impl_t *hal, wx_cloudvoip_config_t *config);

/**
 * @brief 销毁 voip
 * 
 * 进行资源释放
 */
void wx_destory(void);

/**
 * @brief 注册设备
 * 
 * 需要调用一次此接口,若设备已经注册,则立即返回,若设备未注册或注册数据错误,会再注册。
 *
 * @param sn_ticket (nonnull) 小程序对应的 snticket,参考:
 * https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/hardware-device/getSnTicket.html
 * @return
 *  - WXERROR_RESOURCE_EXHAUSTED: 内存资源分配失败
 *  - WXERROR_RESPONSE: 后台返回失败,一般是网络问题
 *  - WXERROR_IO: IO 失败,一般是写文件接口返回失败
 *  - 其它:
 *       -10008: snticket 有问题
 */
wx_error_t wx_device_register(const char *sn_ticket);

/**
 * @brief 检测设备是否已经被注册
 * 需要在 wx_init 调用完成之后才能调用本函数.
 *
 * @param is_registered_out 输出设备注册情况.
 * @return 检测过程中是否出现错误, 比如数据文件夹不可读写, 或者 rpmb
 * 设备不能被正常访问.
 *   - WXERROR_OK: 正确返回, 可以正确使用 is_registered_out 的值来判断设备是否注册过
 *   - WXERROR_FAILED_PRECONDITION: wx_init 未被调用
 *   - WXERROR_INVALID_ARGUMENT: 参数为空
 *   - WXERROR_INVALID_DEVICEID: 设备已注册,但当前 sdk 用的 deviceid、modelid 不对
 *   - WXERROR_UNKNOWN: 注册信息被破坏,这种情况下,一般可以清理设备重新注册
 */
wx_error_t wx_device_is_registered(int* is_registered_out);

/**
 * @brief 拨打 VoIP 通话给指定的微信用户
 *
 * 厂商需要自行实现用户的通讯录功能.
 * 厂商需要在自己的小程序内提示用户向指定的设备 (设备 ID) 授权 VoIP 通话权限后,
 * 方可在携带该设备 ID 的设备上向该用户拨打 VoIP 通话.
 *
 * @param room_type 通话类型,有音频和音视频两种
 * @param caller 本设备的 VoIP 角色设置.
 * @param callee 要拨打的微信用户的 VoIP 角色设置
 * @param custom_query 小程序页面自定义参数, 可以传 NULL
 * @param payload 第三方云 SDK 标记 VoIP 流的标识符(比如 json)
 * @return
 *   - WXERROR_FAILED_PRECONDITION: 不能发起通话请求,比如获取 token 不对等.
 *   - WXERROR_RESOURCE_EXHAUSTED: 内存资源分配失败
 *   - WXERROR_RESPONSE: 后台返回失败,一般是网络问题
 *   - 负数:请求通话时后台返回了失败码取负,可参考https://mp.weixin.qq.com/wxopen/plugindevdoc?appid=wxf830863afde621eb
 *       1	roomid 错误
 *       2	设备 deviceId 错误
 *       3	voip_id 错误
 *       4	校园场景支付刷脸模式,voipToken 错误
 *       5	生成 voip 房间错误
 *       7	openId 错误
 *       8	openId 未授权
 *       9	校园场景支付刷脸模式:openId 不是 userId 的联系人;硬件设备模式:openId 未绑定设备
 *       12	小程序音视频能力审核未完成,正式版中暂时无法使用
 *       13	硬件设备拨打手机微信模式,voipToken 错误
 *       14	手机微信拨打硬件设备模式,voipToken 错误
 *       15	音视频费用包欠费
 *       17	voipToken 对应 modelId 错误
 *       19	openId 与小程序 appId 不匹配。请注意同一个用户在不同小程序的 openId 是不同的
 *       20	openId 无效
 *       10008 snticket 过期
 */
wx_error_t
wx_cloudvoip_client_call(wx_cloudvoip_session_type_t room_type,
                         const wx_cloudvoip_member_t* caller,
                         const wx_cloudvoip_member_t* callee,
                         const char* custom_query,
                         const char* payload);

/**
 * @brief 微信拨打设备,设备使用 roomid 加入通话
 *
 * 厂商需要自行将小程序端呼叫设备时的 roomid 流转到设备上.
 *
 * @param roomid 微信小程序端插件发起通话后,获取本次通话的roomid
 * @param payload 第三方云 SDK 标记 VoIP 流的标识符(比如 json)
 * @return
 *   - WXERROR_FAILED_PRECONDITION: 不能发起通话请求,比如获取 token 不对等.
 *   - WXERROR_RESOURCE_EXHAUSTED: 内存资源分配失败
 *   - WXERROR_RESPONSE: 后台返回失败,一般是网络问题
 *   - 负数:请求通话时后台返回了失败码取负,可参考https://developers.weixin.qq.com/miniprogram/dev/framework/device/voip-plugin/api/errCode.html
 *       1	roomid 错误
 *       2	设备 deviceId 错误
 *       3	voip_id 错误
 *       4	校园场景支付刷脸模式,voipToken 错误
 *       5	生成 voip 房间错误
 *       7	openId 错误
 *       8	openId 未授权
 *       9	校园场景支付刷脸模式:openId 不是 userId 的联系人;硬件设备模式:openId 未绑定设备
 *       12	小程序音视频能力审核未完成,正式版中暂时无法使用
 *       13	硬件设备拨打手机微信模式,voipToken 错误
 *       14	手机微信拨打硬件设备模式,voipToken 错误
 *       15	音视频费用包欠费
 *       17	voipToken 对应 modelId 错误
 *       19	openId 与小程序 appId 不匹配。请注意同一个用户在不同小程序的 openId 是不同的
 *       20	openId 无效
 */
wx_error_t 
wx_cloudvoip_client_join(const char* roomid,
                          const char* payload);