1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include "b8_views.h" 15 #include "ble_lab.h" 16 #include "central.h" 17 18 namespace { 19 20 b8 :: Central central ; 21 b8v :: Pick pick ; 22 ble :: Heard rows [ ble :: ScanTable :: kMax ] ; 23 int rowCount = 0 ; 24 int scroll = 0 ; 25 b8 :: Progress prog ; 26 b8 :: Input in ; 27 b8 :: Tree tr ; 28 29 const ble :: Heard * picked ( ) { 30 for ( int i = 0 ; i < rowCount ; i ++ ) 31 if ( pick . matches ( rows [ i ] ) ) return & rows [ i ] ; 32 return nullptr ; 33 } 34 35 36 37 bool connectedNow ( ) { return central . connected ( ) ; } 38 39 void drawDevices ( BleLab & , FrameBuffer & fb , float t ) { b8v :: devices ( fb , rows , rowCount , pick , central , prog , t ) ; } 40 void drawExplore ( BleLab & , FrameBuffer & fb , float t ) { 41 b8 :: tree ( ) . copyTo ( tr ) ; 42 b8v :: explore ( fb , tr , scroll , prog , t ) ; 43 } 44 void drawKeyboard ( BleLab & , FrameBuffer & fb , float t ) { 45 b8 :: input ( ) . copyTo ( in ) ; 46 b8v :: keyboard ( fb , in , prog , connectedNow ( ) , central . hid . layout ( ) , t ) ; 47 } 48 void drawMouse ( BleLab & , FrameBuffer & fb , float t ) { 49 b8 :: input ( ) . copyTo ( in ) ; 50 b8v :: mouse ( fb , in , prog , connectedNow ( ) , t ) ; 51 } 52 void drawReports ( BleLab & , FrameBuffer & fb , float t ) { 53 b8 :: input ( ) . copyTo ( in ) ; 54 b8v :: reports ( fb , in , central . hid , prog , connectedNow ( ) , t ) ; 55 } 56 void drawLink ( BleLab & lab , FrameBuffer & fb , float t ) { bv :: link ( fb , lab . state ( ) , t , "this board" ) ; } 57 void drawLog ( BleLab & , FrameBuffer & fb , float ) { bv :: log ( fb ) ; } 58 59 const BleView kViews [ ] = { 60 { "devices" , drawDevices , "next" } , 61 { "explore" , drawExplore , "more" } , 62 { "keyboard" , drawKeyboard , "clear" } , 63 { "mouse" , drawMouse , "clear" } , 64 { "reports" , drawReports , nullptr } , 65 { "link" , drawLink , nullptr } , 66 { "log" , drawLog , nullptr } , 67 } ; 68 69 70 71 void cmdScan ( BleLab & lab , const String & , const String & lower ) { 72 if ( ! lower . isEmpty ( ) && lower != "on" && lower != "off" ) return lab . warn ( "scan | scan on | scan off" ) ; 73 const bool on = lower . isEmpty ( ) ? ! NimBLEDevice :: getScan ( ) -> isScanning ( ) : lower == "on" ; 74 if ( on && central . connected ( ) ) return lab . warn ( "connected: disconnect first (one link at a time in this lab)" ) ; 75 if ( on ) central . startScan ( ) , BLE_NOTE ( Info , "scanning" ) ; 76 else central . stopScan ( ) , BLE_NOTE ( Info , "scan paused" ) ; 77 } 78 79 void cmdList ( BleLab & , const String & , const String & ) { 80 Serial . printf ( "%d connectable devices (strongest first):\n" , rowCount ) ; 81 for ( int i = 0 ; i < rowCount ; i ++ ) { 82 const ble :: Heard & h = rows [ i ] ; 83 char addr [ 18 ] ; 84 h . addrText ( addr , sizeof ( addr ) ) ; 85 Serial . printf ( "%3d %4d dBm %-14s %s %s%s%s\n" , i + 1 , int ( h . rssiAvg ) , ad :: kindName ( h . kind ) , addr , h . label ( ) , 86 h . kind == ad :: Kind :: Input ? " [HID]" : "" , central . isBonded ( h . addr , h . addrType ) ? " [bonded]" : "" ) ; 87 } 88 } 89 90 void pickIndex ( int i ) { 91 if ( i < 0 || i >= rowCount ) return ; 92 pick . take ( rows [ i ] ) ; 93 BLE_NOTE ( Info , "picked %d: %s" , i + 1 , rows [ i ] . label ( ) ) ; 94 } 95 96 void cmdNext ( BleLab & , const String & , const String & ) { 97 if ( ! rowCount ) return ; 98 int at = - 1 ; 99 for ( int i = 0 ; i < rowCount ; i ++ ) 100 if ( pick . matches ( rows [ i ] ) ) at = i ; 101 pickIndex ( ( at + 1 ) % rowCount ) ; 102 } 103 104 void cmdConnect ( BleLab & lab , const String & arg , const String & ) { 105 long n = 0 ; 106 if ( ! arg . isEmpty ( ) ) { 107 if ( ! BleLab :: number ( arg , 1 , std :: max ( rowCount , 1 ) , n ) ) return lab . warn ( "connect <1-%d> (the number from list)" , rowCount ) ; 108 pickIndex ( n - 1 ) ; 109 } 110 const ble :: Heard * h = picked ( ) ; 111 if ( ! h ) return lab . warn ( "nothing picked: devices view double-click, or connect <n>" ) ; 112 if ( central . connected ( ) ) return lab . warn ( "already connected: disconnect first" ) ; 113 b8 :: Request r ; 114 r . job = b8 :: Job :: Connect ; 115 memcpy ( r . addr , h -> addr , 6 ) ; 116 r . addrType = h -> addrType ; 117 strlcpy ( r . name , h -> label ( ) , sizeof ( r . name ) ) ; 118 scroll = 0 ; 119 central . submit ( r ) ; 120 } 121 122 void simple ( b8 :: Job job ) { 123 b8 :: Request r ; 124 r . job = job ; 125 central . submit ( r ) ; 126 } 127 128 void cmdDisconnect ( BleLab & lab , const String & , const String & ) { 129 if ( ! central . connected ( ) ) return lab . warn ( "nothing connected" ) ; 130 simple ( b8 :: Job :: Disconnect ) ; 131 } 132 void cmdExplore ( BleLab & lab , const String & , const String & ) { 133 if ( ! central . connected ( ) ) return lab . warn ( "connect first" ) ; 134 scroll = 0 ; 135 simple ( b8 :: Job :: Explore ) ; 136 } 137 void cmdPair ( BleLab & lab , const String & , const String & ) { 138 if ( ! central . connected ( ) ) return lab . warn ( "connect first" ) ; 139 simple ( b8 :: Job :: Pair ) ; 140 } 141 void cmdHost ( BleLab & lab , const String & , const String & ) { 142 if ( ! central . connected ( ) ) return lab . warn ( "connect a keyboard or mouse first" ) ; 143 simple ( b8 :: Job :: Host ) ; 144 } 145 void cmdForget ( BleLab & , const String & , const String & ) { simple ( b8 :: Job :: Forget ) ; } 146 147 void cmdBoot ( BleLab & lab , const String & , const String & lower ) { 148 if ( lower != "on" && lower != "off" ) return lab . warn ( "boot on | boot off (now %s)" , central . boot ( ) ? "on" : "off" ) ; 149 central . setBoot ( lower == "on" ) ; 150 BLE_NOTE ( Info , "%s protocol from the next host" , central . boot ( ) ? "boot" : "report" ) ; 151 if ( prog . phase == b8 :: Phase :: Hosting ) simple ( b8 :: Job :: Boot ) ; 152 } 153 154 void cmdLayout ( BleLab & lab , const String & , const String & lower ) { 155 if ( lower != "us" && lower != "de" ) return lab . warn ( "layout us | layout de (the layout your keyboard is printed with)" ) ; 156 central . hid . setLayout ( lower == "de" ? hid :: Layout :: De : hid :: Layout :: Us ) ; 157 BLE_NOTE ( Info , "layout %s: the same keys, other letters" , lower == "de" ? "German (QWERTZ)" : "US (QWERTY)" ) ; 158 } 159 160 void cmdClear ( BleLab & lab , const String & , const String & ) { 161 b8 :: input ( ) . edit ( [ ] ( b8 :: Input & i ) { 162 i . len = 0 ; 163 i . strokeHead = 0 ; 164 } ) ; 165 ( void ) lab ; 166 } 167 168 void cmdMore ( BleLab & , const String & , const String & ) { 169 b8 :: tree ( ) . copyTo ( tr ) ; 170 scroll = scroll + 8 >= tr . count ? 0 : scroll + 8 ; 171 } 172 173 void cmdYes ( BleLab & lab , const String & , const String & ) { 174 if ( ! central . links . answer ( central . client ( ) , true ) ) lab . warn ( "no numeric comparison is waiting" ) ; 175 } 176 void cmdNo ( BleLab & lab , const String & , const String & ) { 177 if ( ! central . links . answer ( central . client ( ) , false ) ) lab . warn ( "no numeric comparison is waiting" ) ; 178 } 179 void cmdPasskey ( BleLab & lab , const String & arg , const String & ) { 180 long pin = 0 ; 181 if ( ! BleLab :: number ( arg , 0 , 999999 , pin ) ) return lab . warn ( "passkey <6 digits shown on the device>" ) ; 182 if ( ! central . links . enter ( central . client ( ) , uint32_t ( pin ) ) ) lab . warn ( "no passkey entry is waiting" ) ; 183 } 184 185 const BleCommand kCommands [ ] = { 186 ⋯ 16 lines of data folded away. Show them 202 } ; 203 204 205 206 void setup ( BleLab & ) { central . begin ( ) ; } 207 208 void tick ( BleLab & lab ) { 209 210 const int n = central . table . snapshot ( rows , ble :: ScanTable :: kMax , 20000 ) ; 211 rowCount = 0 ; 212 for ( int i = 0 ; i < n ; i ++ ) 213 if ( rows [ i ] . connectable ) rows [ rowCount ++ ] = rows [ i ] ; 214 if ( ! pick . set && rowCount ) pick . take ( rows [ 0 ] ) ; 215 b8 :: progress ( ) . copyTo ( prog ) ; 216 const int want = central . wantView ; 217 if ( want >= 0 ) { 218 central . wantView = - 1 ; 219 lab . setView ( want ) ; 220 } 221 } 222 223 224 bool gesture ( BleLab & lab , Gesture g , bool ) { 225 if ( central . links . comparing ( ) && g != Gesture :: DoubleClick ) { 226 central . links . answer ( central . client ( ) , g == Gesture :: Click ) ; 227 return true ; 228 } 229 if ( g != Gesture :: LongPress ) return false ; 230 b8 :: tree ( ) . copyTo ( tr ) ; 231 const char * next = ! central . connected ( ) ? "connect" : ( tr . hasHid && prog . phase != b8 :: Phase :: Hosting ) ? "host" : "disconnect" ; 232 Serial . printf ( "> %s (long press)\n" , next ) ; 233 lab . runLine ( next ) ; 234 return true ; 235 } 236 237 const BleLabSpec kSpec { 238 "ble-lab-b8" , "B8" , "Host" , "Bench B8" , kViews , sizeof ( kViews ) / sizeof ( kViews [ 0 ] ) , 0 , 239 kCommands , sizeof ( kCommands ) / sizeof ( kCommands [ 0 ] ) , setup , tick , gesture , nullptr , 240 } ; 241 242 BleLab lab ( kSpec ) ; 243 244 } 245 246 void setup ( ) { lab . begin ( ) ; } 247 248 void loop ( ) { lab . loop ( ) ; }