Browse Source

Debugging serial

Thomas Buck 12 years ago
parent
commit
50bfa7908a
2 changed files with 23 additions and 15 deletions
  1. 7
    11
      CubeControl/helper/unixSerial.c
  2. 16
    4
      CubeControl/serialHelper.c

+ 7
- 11
CubeControl/helper/unixSerial.c View File

@@ -114,7 +114,7 @@ char** namesInDev(int *siz) {
114 114
 		size++;
115 115
 	}
116 116
 	files = (char **)malloc((size + 1) * sizeof(char *));
117
-	files[size] = NULL;
117
+	files[size++] = NULL;
118 118
 	closedir(dir);
119 119
 	dir = opendir("/dev/");
120 120
 	while ((ent = readdir(dir)) != NULL) {
@@ -126,7 +126,7 @@ char** namesInDev(int *siz) {
126 126
 
127 127
 	char *tmp = NULL;
128 128
 	// Fix every string, addin /dev/ in front of it...
129
-	for (i = 0; i < size; i++) {
129
+	for (i = 0; i < (size - 1); i++) {
130 130
 		tmp = (char *)malloc((strlen(files[i]) + 6) * sizeof(char));
131 131
 		tmp[0] = '/';
132 132
 		tmp[1] = 'd';
@@ -146,8 +146,10 @@ char** getSerialPorts(void) {
146 146
 	char** fin = NULL;
147 147
 	int i = 0, j = 0, f, g;
148 148
 
149
-	fin = (char **)malloc((size + 1) * sizeof(char *));
150
-	fin[size] = NULL;
149
+	printf("JNI: Got files in /dev/\n");
150
+
151
+	fin = (char **)malloc(size * sizeof(char *));
152
+	fin[size - 1] = NULL;
151 153
 	while (files[i] != NULL) {
152 154
 		if (strstr(files[i], SEARCH) != NULL) {
153 155
 			f = serialOpen(files[i]);
@@ -160,11 +162,5 @@ char** getSerialPorts(void) {
160 162
 	}
161 163
 	free(files);
162 164
 
163
-	char** finish = (char **)malloc((j + 1) * sizeof(char *));
164
-	for (i = 0; i < (j + 1); i++) {
165
-		finish[i] = fin[i];
166
-	}
167
-	free(fin);
168
-
169
-	return finish;
165
+	return fin;
170 166
 }

+ 16
- 4
CubeControl/serialHelper.c View File

@@ -34,19 +34,31 @@
34 34
 JNIEXPORT jstring JNICALL Java_HelperUtility_getPorts(JNIEnv *env, jclass class) {
35 35
 	char **ports = getSerialPorts();
36 36
 	char *string = NULL;
37
-	int length = 0, leng2 = 0, lengthabs = 0;;
37
+	int length = 0, leng2 = 0, lengthabs = 0;
38
+
39
+	printf("JNI: Got serial ports...\n");
40
+
41
+	// Count how much memory we need for string of all ports, with \n in between
38 42
 	while (ports[length] != NULL) {
43
+		printf("JNI: Startin count...\n");
39 44
 		while (ports[length][leng2] != '\0') {
40 45
 			leng2++;
41 46
 		}
47
+		printf("JNI: Counted %s\n", ports[length]);
42 48
 		lengthabs += leng2;
43 49
 		leng2 = 0;
44 50
 		length++;
45 51
 	}
46
-	
47 52
 	length += lengthabs;
48
-	string = (char *)malloc((length * sizeof(char)) + 1);
49
-	
53
+
54
+	printf("JNI: Counted serial ports...\n");
55
+
56
+	string = (char *)malloc((length + 1) * sizeof(char));
57
+	if (string == NULL) {
58
+		printf("JNI: Not enough memory!\n");
59
+		return (*env)->NewStringUTF(env, NULL);
60
+	}
61
+
50 62
 	length = 0;
51 63
 	lengthabs = 0;
52 64
 	while (ports[length] != NULL) {

Loading…
Cancel
Save