From: liangzhenying <867174124@qq.com> Date: Fri, 16 Aug 2024 07:01:57 +0000 (+0800) Subject: 阿里鉴权去掉productkey判断 X-Git-Tag: 千寻适配d9~49 X-Git-Url: http://git.snicker.com.cn/css/static/gitweb.css?a=commitdiff_plain;h=9b1e3dd79f229d24633292260aefe1aa03156e78;p=mirrors%2FMouseSdk-MacOS.git 阿里鉴权去掉productkey判断 --- diff --git "a/Document/AiMouseSdk\346\216\245\345\217\243\346\226\207\346\241\243.md" "b/Document/AiMouseSdk\346\216\245\345\217\243\346\226\207\346\241\243.md" index dd7784f..14ab19f 100644 --- "a/Document/AiMouseSdk\346\216\245\345\217\243\346\226\207\346\241\243.md" +++ "b/Document/AiMouseSdk\346\216\245\345\217\243\346\226\207\346\241\243.md" @@ -2,24 +2,17 @@ -## 文档修改记录 +## 介绍 -| 版本 | 修改内容 | 修改人 | 修改时间 | -| ------ | --------------------------------------------------- | ------ | ---------- | -| 1.0.0 | 初始版本 | 梁振营 | 2024-03-8 | -| 1.0.1 | 鼠标按键指令修改 | 梁振营 | 2024-03-18 | -| 1.0.2 | 鉴权流程修改,只有鉴权成功了,SDK相关功能才可以使用 | 梁振营 | 2024-03-19 | -| 1.0.3 | 按键指令修改 | 梁振营 | 2024-03-20 | -| 1.0.4 | 支持USB通讯 | 梁振营 | 2024-04-01 | -| 1.0.5 | 支持静态库.a | 梁振营 | 2024-04-22 | -| 1.0.6 | 增加鉴权 | 梁振营 | 2024-04-30 | -| 1.0.13 | 鉴权流程优化 | 梁振营 | 2024-05-20 | -| 1.0.17 | 增加命令控制录音功能 | 梁振营 | 2024-05-28 | -| 1.0.19 | 新增连接状态回调 | 梁振营 | 2024-05-29 | -| 1.0.21 | 鉴权逻辑优化,隐藏鉴权接口 | 梁振营 | 2024-05-31 | -| 1.0.21 | 新增设置品牌接口、USB数据异常接口回调 | 梁振营 | 2024-06-06 | +当前版本:v1.0.31 +更新内容20240816: +1. 支持阿里鉴权 + +## 环境要求 + +MacOS 13.0 @@ -56,7 +49,6 @@ libz.tbd、libbz2.tbd、libiconv.tbd、liblzma5.tbd、CoreAudio.framework、Vide ## SDK相关接口 ```objective-c - /// 单利 +(AiMouse *)shared; @@ -195,6 +187,46 @@ libz.tbd、libbz2.tbd、libiconv.tbd、liblzma5.tbd、CoreAudio.framework、Vide ``` +## 其他类型数据 + +```objective-c + +@interface AiMouseDeviceInfo : NSObject +@property (nonatomic, strong) NSString *battery_power;/// 电池百分百电量,0-100 +@property (nonatomic, strong) NSString *CPI;/// CPI值 +@property (nonatomic, strong) NSString *CPI_list;/// 可设置的CPI参数列表 +@property (nonatomic, strong) NSString *version;///软件版本 +@property (nonatomic, strong) NSString *brand;/// 品牌名称 +@property (nonatomic, strong) NSString *sn;/// 设备序列号 +@property (nonatomic, strong) NSString *mac;///设备MAC地址 +@end + +typedef NS_ENUM(NSInteger,AiMouseErrorCode) { + AiMouseErrorCode_timeOut = 1001,///请求超时 + AiMouseErrorCode_busy = 1002,/// 繁忙,有命令未执行完 + AiMouseErrorCode_unknown = 1003,/// 未知错误 +}; + +/// 日志等级 +typedef NS_ENUM(NSInteger,AiMouseLogLevel) { + AiMouseLogLevel_none = 0, + AiMouseLogLevel_error, + AiMouseLogLevel_info, +}; + +typedef NS_ENUM(NSInteger,AiMouseConnectType) { + AiMouseConnectType_bluetooth = 0,///蓝牙 + AiMouseConnectType_hid,///hid +}; + +typedef NS_ENUM(NSInteger,AiMouseAuthState) { + AiMouseAuthState_Unknow = 0,///未知 + AiMouseAuthState_Success,///鉴权成功 + AiMouseAuthState_Failure///鉴权失败 +}; + +``` + ## 蓝牙示例 @@ -292,7 +324,8 @@ libz.tbd、libbz2.tbd、libiconv.tbd、liblzma5.tbd、CoreAudio.framework、Vide if(self.didDisConnectDevice){ self.didDisConnectDevice(peripheral); } - [self autoConnectDevice]; +// [self aiMouseConnectStateDidChange:false]; + [[AiMouse shared] didDisconnect]; } - (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error{ [self showLog:[NSString stringWithFormat:@"连接失败。。。%@",error.description]]; @@ -321,15 +354,13 @@ libz.tbd、libbz2.tbd、libiconv.tbd、liblzma5.tbd、CoreAudio.framework、Vide [AiMouse shared].delegate = self; //切换当前连接类型 [[AiMouse shared] switchConnect:AiMouseConnectType_bluetooth]; - //找到鉴权特征后,一定要调用鉴权接口开始鉴权。只有鉴权通过了,sdk相关功能才可以使用 - [self startAuthentication]; + } } } //收到蓝牙数据 - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error { NSData *data = characteristic.value; - [self showLog:[NSString stringWithFormat:@"[receive] %@",data]]; if([characteristic.UUID.UUIDString isEqualToString:kAiUUID]){ //AI数据 [[AiMouse shared] sendAiDataToSdk:data]; @@ -341,11 +372,18 @@ libz.tbd、libbz2.tbd、libiconv.tbd、liblzma5.tbd、CoreAudio.framework、Vide } #pragma mark - AiDelegate - +/// 设备鉴权结果返回 +/// - Parameter authState: 鉴权状态 +-(void)aiMouseAuthenticationResult:(AiMouseAuthState)authState{ + [self showLog:[NSString stringWithFormat:@"鉴权结果 = %@",@(authState)]]; + if(self.authCallBack){ + self.authCallBack(authState == AiMouseAuthState_Success); + } +} /// 设备信息数据回调,需要将此数据通过设备信息特征转发给蓝牙设备 /// - Parameter data: 数据 -(void)aiMouseSendDeviceInfoData:(NSData *)data{ - [self showLog:[NSString stringWithFormat:@"[send] %@",data]]; +// [self showLog:[NSString stringWithFormat:@"[send] %@",data]]; [self.connectPeripheral writeValue:data forCharacteristic:self.deviceInfoCha type:CBCharacteristicWriteWithoutResponse]; } @@ -375,7 +413,20 @@ libz.tbd、libbz2.tbd、libiconv.tbd、liblzma5.tbd、CoreAudio.framework、Vide - (void)aiMouseSendRecordData:(NSData *)data{ [self.recordData appendData:data]; } +/// 输出日志 +- (void)aiMouseAddLog:(NSString *)log { +// [self showLog:log]; +} +// 连接状态改变 +- (void)aiMouseConnectStateDidChange:(BOOL)connected{ + if (connected) { + [[NSNotificationCenter defaultCenter] postNotificationName:kDidConnectBleNotification object:nil]; + } + else{ + [[NSNotificationCenter defaultCenter] postNotificationName:kDidDisconnectBleNotification object:nil]; + } +} #pragma mark - public /// 连接设备 - (void)startScanDevice{ @@ -397,15 +448,7 @@ libz.tbd、libbz2.tbd、libiconv.tbd、liblzma5.tbd、CoreAudio.framework、Vide - (void)getDeviceInfo:(void (^)(AiMouseDeviceInfo * _Nonnull, NSError * _Nonnull))completation{ [[AiMouse shared] getDeviceInfo:completation]; } -/// 鉴权 --(void)startAuthentication{ - [[AiMouse shared] startAuthentication:^(AiMouseAuthState state) { - [self showLog:[NSString stringWithFormat:@"鉴权结果 = %@",@(state)]]; - if(self.authCallBack){ - self.authCallBack(state == AiMouseAuthState_Success); - } - }]; -} + #pragma mark - other //自动连接,获取已配对的蓝牙,取第一个连接 - (void)autoConnectDevice{ @@ -510,7 +553,7 @@ const long productID = 0x8601; - (void)open{ self.hidManager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone); - IOHIDManagerScheduleWithRunLoop(self.hidManager, CFRunLoopGetMain(), kCFRunLoopDefaultMode); + IOHIDManagerScheduleWithRunLoop(self.hidManager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); IOReturn ret = IOHIDManagerOpen(self.hidManager, 0L); if (ret != kIOReturnSuccess) { NSLog(@"打开设备失败!"); @@ -540,28 +583,40 @@ const long productID = 0x8601; #pragma mark - Hid CallBack /// 匹配到设备回调 static void hidAppearDevice(void *inContext,IOReturn inResult,void *inSender,IOHIDDeviceRef inIOHIDDeviceRef) { - uint32_t vid = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDVendorIDKey)); - uint32_t pid = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDProductIDKey)); + [[HidManager shared] showLog:[NSString stringWithFormat:@"发现设备 %@\n ",inIOHIDDeviceRef]]; uint32_t PrimaryUsagePage = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDPrimaryUsagePageKey)); uint32_t PrimaryUsage = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDPrimaryUsageKey)); - [[HidManager shared] showLog:[NSString stringWithFormat:@"vid = %u, pid = %u, PrimaryUsagePage = %u, PrimaryUsage = %u,",vid,pid,PrimaryUsagePage,PrimaryUsage]]; - [[HidManager shared] showLog:[NSString stringWithFormat:@"发现设备 %@\n ",inIOHIDDeviceRef]]; - - // PrimaryUsagePage=1 PrimaryUsage=0,设备信息 - // PrimaryUsagePage=65283 PrimaryUsage=0,ai if(PrimaryUsagePage == 1 && PrimaryUsage == 0){ //设备信息 [HidManager shared].deviceInfoCha = inIOHIDDeviceRef; - //开始鉴权 - [[HidManager shared] startAuthentication]; + [[HidManager shared] connectDevice]; + //注册消息回调 + unsigned char InputBuffer[128]; + IOHIDDeviceRegisterInputReportCallback(inIOHIDDeviceRef, InputBuffer, sizeof(InputBuffer), hidReceiveData, NULL); + IOReturn ret = IOHIDDeviceOpen(inIOHIDDeviceRef, 0); + if (ret != kIOReturnSuccess) { + NSLog(@"打开设备信息失败!"); + }else{ + NSLog(@"打开设备信息成功!"); + } + CFRetain(inIOHIDDeviceRef); + IOHIDDeviceScheduleWithRunLoop(inIOHIDDeviceRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); } else if(PrimaryUsagePage == 65283 && PrimaryUsage == 0){ //ai [HidManager shared].aiCha = inIOHIDDeviceRef; + //注册消息回调 + unsigned char InputBuffer[128]; + IOHIDDeviceRegisterInputReportCallback(inIOHIDDeviceRef, InputBuffer, sizeof(InputBuffer), hidReceiveData, NULL); + IOReturn ret = IOHIDDeviceOpen(inIOHIDDeviceRef, 0); + if (ret != kIOReturnSuccess) { + NSLog(@"打开AI失败!"); + }else{ + NSLog(@"打开AI成功!"); + } + CFRetain(inIOHIDDeviceRef); + IOHIDDeviceScheduleWithRunLoop(inIOHIDDeviceRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); } - //注册消息回调 - unsigned char InputBuffer[128]; - IOHIDDeviceRegisterInputReportCallback(inIOHIDDeviceRef, InputBuffer, sizeof(InputBuffer), hidReceiveData, NULL); } /// 设备移除回调 @@ -571,7 +626,9 @@ static void hidDisAppearDevice(void *inContext,IOReturn inResult,void *inSender, uint32_t PrimaryUsage = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDPrimaryUsageKey)); if(PrimaryUsagePage == 1 && PrimaryUsage == 0){ - [HidManager shared].connectStateCallBack(false); + [HidManager shared].deviceInfoCha = nil; + [HidManager shared].aiCha = nil; + [[AiMouse shared] didDisconnect]; } } @@ -586,11 +643,10 @@ static void hidReceiveData(void* context, IOReturn result, void* sender, IOHIDRe uint32_t PrimaryUsage = IOHIDDevGetUInt32Property(sender, CFSTR(kIOHIDPrimaryUsageKey)); if(PrimaryUsagePage == 1 && PrimaryUsage == 0){ - [[HidManager shared] showLog:[NSString stringWithFormat:@"收到device数据 %@",data]]; [[AiMouse shared] sendDeviceDataToSdk:data]; + } else if(PrimaryUsagePage == 65283 && PrimaryUsage == 0){ - [[HidManager shared] showLog:[NSString stringWithFormat:@"收到AI数据 %@",data]]; [[AiMouse shared] sendAiDataToSdk:data]; } } @@ -601,6 +657,14 @@ static void hidReceiveData(void* context, IOReturn result, void* sender, IOHIDRe [self sendData:data toDevice:self.deviceInfoCha]; } +/// 设备鉴权结果返回 +/// - Parameter authState: 鉴权状态 +-(void)aiMouseAuthenticationResult:(AiMouseAuthState)authState{ + [self showLog:[NSString stringWithFormat:@"鉴权结果 = %@",@(authState)]]; + if(self.authCallBack){ + self.authCallBack(authState == AiMouseAuthState_Success); + } +} /// 鼠标按键指令回调 /// - Parameter data: 数据。 /// 前 第一次单击31(开始录音), 第二次单击32(结束录音) , 按下21(开始录音)抬起22(结束录音) @@ -626,23 +690,26 @@ static void hidReceiveData(void* context, IOReturn result, void* sender, IOHIDRe - (void)aiMouseSendRecordData:(NSData *)data{ [self.recordData appendData:data]; } +/// 输出日志 +- (void)aiMouseAddLog:(NSString *)log { +// [self showLog:log]; +} +-(void)hidDataError{ + [self close]; + [self open]; +} + +// 连接状态改变 +- (void)aiMouseConnectStateDidChange:(BOOL)connected{ + [HidManager shared].connectStateCallBack(connected); +} #pragma mark - public --(void)startAuthentication{ - //连上设备 - [HidManager shared].connectStateCallBack(true); +-(void)connectDevice{ //设置代理 [AiMouse shared].delegate = self; //切换当前连接类型 [[AiMouse shared] switchConnect:AiMouseConnectType_hid]; - //找到鉴权特征后,一定要调用鉴权接口开始鉴权。只有鉴权通过了,sdk相关功能才可以使用 - [self showLog:@"开始鉴权"]; - [[AiMouse shared] startAuthentication:^(AiMouseAuthState state) { - [self showLog:[NSString stringWithFormat:@"鉴权结果 = %@",@(state)]]; - if(self.authCallBack){ - self.authCallBack(state == AiMouseAuthState_Success); - } - }]; } /// 获取设备信息 - (void)getDeviceInfo:(void (^)(AiMouseDeviceInfo * _Nonnull, NSError * _Nonnull))completation{ @@ -655,7 +722,6 @@ static void hidReceiveData(void* context, IOReturn result, void* sender, IOHIDRe [self showLog:@"请先连上设备"]; return; } - [self showLog:[NSString stringWithFormat:@"开始发送[send] %@",data]]; IOReturn ret = IOHIDDeviceSetReport(device, kIOHIDReportTypeOutput, 0, data.bytes, data.length); if (ret != kIOReturnSuccess) { diff --git "a/Document/\351\272\246\345\220\211sdk.rtf" "b/Document/\351\272\246\345\220\211sdk.rtf" new file mode 100644 index 0000000..b40b3d8 --- /dev/null +++ "b/Document/\351\272\246\345\220\211sdk.rtf" @@ -0,0 +1,11 @@ +{\rtf1\ansi\ansicpg936\cocoartf2709 +\cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fswiss\fcharset0 Helvetica;} +{\colortbl;\red255\green255\blue255;} +{\*\expandedcolortbl;;} +\paperw11900\paperh16840\margl1440\margr1440\vieww11520\viewh8400\viewkind0 +\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0 + +\f0\fs24 \cf0 \uc0\u26680 \u33455 \u24494 \u30340 \u25353 \u38190 \u21644 \u36890 \u36947 \u36319 \u23500 \u29790 \u22372 \u19981 \u19968 \u26679 \ +\ +\ +\uc0\u27888 \u20940 \u24494 \u12289 \u40614 \u21513 } \ No newline at end of file diff --git "a/MaiJi/Mouse_Dongle\346\216\245\346\224\266\345\231\250\351\200\232\344\277\241\345\215\217\350\256\256_v1.0_20240619.docx" "b/MaiJi/Mouse_Dongle\346\216\245\346\224\266\345\231\250\351\200\232\344\277\241\345\215\217\350\256\256_v1.0_20240619.docx" new file mode 100644 index 0000000..095418a Binary files /dev/null and "b/MaiJi/Mouse_Dongle\346\216\245\346\224\266\345\231\250\351\200\232\344\277\241\345\215\217\350\256\256_v1.0_20240619.docx" differ diff --git "a/MaiJi/\350\257\255\351\237\263\351\274\240\346\240\207\350\223\235\347\211\231\351\200\232\344\277\241\346\216\245\345\217\243v1.0_20240619.docx" "b/MaiJi/\350\257\255\351\237\263\351\274\240\346\240\207\350\223\235\347\211\231\351\200\232\344\277\241\346\216\245\345\217\243v1.0_20240619.docx" new file mode 100644 index 0000000..fd74c80 Binary files /dev/null and "b/MaiJi/\350\257\255\351\237\263\351\274\240\346\240\207\350\223\235\347\211\231\351\200\232\344\277\241\346\216\245\345\217\243v1.0_20240619.docx" differ diff --git a/MouseSdk/AiMouseSdk/AiMouseSdk/AiAliAuthManager.m b/MouseSdk/AiMouseSdk/AiMouseSdk/AiAliAuthManager.m index b312e5a..53734ee 100644 --- a/MouseSdk/AiMouseSdk/AiMouseSdk/AiAliAuthManager.m +++ b/MouseSdk/AiMouseSdk/AiMouseSdk/AiAliAuthManager.m @@ -36,18 +36,20 @@ const NSString *instanceId = @"iot-040adh4x"; - (void)loginWith:(AiMouseDeviceInfo *)info{ self.deviceInfo = info; - if (![info valueForKey:@"productKey"] || ![info valueForKey:@"secretKey"] || !info.sn || !info.sn) { - [self showLog:@"获取设备信息错误,鉴权失败!"]; - return; - } - if([[info valueForKey:@"deviceSecret"] isKindOfClass:[NSString class]] && ([(NSString *)[info valueForKey:@"deviceSecret"] length] > 2)){ - //已注册过,直接登录 - [self loginRequest]; + if ([self.deviceInfo.mac isKindOfClass:[NSString class]] && self.deviceInfo.mac.length > 0 ) { + if([[info valueForKey:@"deviceSecret"] isKindOfClass:[NSString class]] && ([(NSString *)[info valueForKey:@"deviceSecret"] length] > 2)){ + //已注册过,直接登录 + [self loginRequest]; + } + else{ + //未注册 + [self registerReuest]; + } } else{ - //未注册 - [self registerReuest]; + [self showLog:@"获取设备信息错误,鉴权失败!"]; } + } - (void)showLog:(NSString *)log { @@ -97,14 +99,11 @@ const NSString *instanceId = @"iot-040adh4x"; } - (void)registerReuest{ -// C0310000A58A 8228bd13d8ef4bed091a9f3d6c384b51 -// C0310000A58A 62c5bfb84f79e387036c29a2e0ceef80 NSMutableDictionary *params = [NSMutableDictionary dictionaryWithDictionary:@{ @"Action":@"RegisterDevice", @"ProductKey":productKey, @"IotInstanceId":instanceId, -// @"DeviceName":@"C0310000A59C", @"DeviceName": self.deviceInfo.mac, }] ; params[@"Format"] = @"JSON"; diff --git a/MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate b/MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate index 79d7155..926899f 100644 Binary files a/MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate and b/MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate differ