/
SSH.m
150 lines (120 loc) · 4.19 KB
/
SSH.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#import "SSH.h"
#import <NMSSH/NMSSH.h>
#define RPI_KEY @“******”
#define RPI_IP @“***.***.**.***”
#define RPI_PORT ****
#define RPI_USERNAME @“*****”
#define RPI_HDD @"/media/usbhdd/%@"
@interface SSH() <NMSSHSessionDelegate>
{
NMSSHChannel *channel;
NMSSHSession *session;
dispatch_queue_t queue;
}
@end
@implementation SSH
-(instancetype)init
{
self = [super init];
//check for rsa key
NSString *filePath = [[NSBundle mainBundle] pathForResource:RPI_KEY ofType:@""];
NSString *rsa = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
if (rsa) {
//create instances of channel and session
session = [[NMSSHSession alloc] initWithHost:RPI_IP port:RPI_PORT andUsername:RPI_USERNAME];
if (![session isConnected]) {
[session connect];
}
if (session.isConnected) {
[session authenticateByPublicKey:nil privateKey:filePath andPassword:nil];
if (session.isAuthorized) {
channel = [[NMSSHChannel alloc] initWithSession:session];
}
}
}
//create dispatch queue
queue = dispatch_queue_create("QueueName", NULL);
return self;
}
-(BOOL)isConnected
{
return session.isConnected;
}
-(void)disconnect
{
[session disconnect];
}
-(void)connect
{
[session connect];
}
//take the command string and any strings inside args array and form a proper
//shell terminal command
//ex. 'python /filePathToScript/ arg1 arg2'
-(void)sendCommand:(NSString *)command withArguments:(NSArray *)args
{
if (args.count > 0) {
//create string for arguments
NSMutableString *base = [[NSMutableString alloc] initWithString:command];
//iterate thru each argument in array
for (NSString *argument in args){
//append base with new argument
[base appendString:[NSString stringWithFormat:@" \"%@\"",argument]];
}
//update command string with arguments
command = base;
NSLog(@"final command: %@", command);
}
dispatch_async(queue, ^{
NSError *err;
[channel execute:command error:&err];
if (err) {
NSLog(@"error:%@", err.description);
self.isArduinoOnline = NO;
}
});
}
-(void)downloadFile:(NSString *)fileName
{
//create path for file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePathStatus = [documentsDirectory stringByAppendingPathComponent:fileName];
//raspberry pi file directory
NSString *rpiFileName = [NSString stringWithFormat:RPI_HDD, fileName];
//downloads should hold up thread until completed
//other routines require the downloaded file before operation
dispatch_sync(queue, ^{
BOOL t = [channel downloadFile:rpiFileName to:filePathStatus];
if (t != 1) {
NSLog(@"error");
self.isArduinoOnline = NO;
}else{
self.isArduinoOnline = YES;
}
//sanity check for file
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:filePathStatus]) {
NSLog(@"file not found");
}
});
}
-(void)uploadFile:(NSString *)filePath
{
//get audio file name
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *directoryPath = [documentsDirectory stringByAppendingString:@"/"];
NSString *fileName = [filePath stringByReplacingOccurrencesOfString:directoryPath withString:@""];
//raspberry pi file directory
NSString *rpiFileName = [NSString stringWithFormat:RPI_HDD, fileName];
//downloads should hold up thread until completed
//other routines require the downloaded file before operation
dispatch_sync(queue, ^{
BOOL t = [channel uploadFile:filePath to:rpiFileName];
if (t != 1) {
NSLog(@"error");
}
});
}
@end