Merge pull request #201 from lakinduakash/dev

Add libqrencode for qr generation instead of cli
This commit is contained in:
Lakindu Akash 2021-10-18 04:20:11 +05:30 committed by GitHub
commit 400bc59512
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 290 additions and 22 deletions

View File

@ -87,11 +87,13 @@ install it using your distro's package manager_
* pkg-config
* gtk
* libgtk-3-dev
* libqrencode-dev (for qr code generation)
* libpng-dev (for qr code generation)
On Ubuntu or debian install dependencies by,
```bash
sudo apt install -y libgtk-3-dev build-essential gcc g++ pkg-config make hostapd
sudo apt install -y libgtk-3-dev build-essential gcc g++ pkg-config make hostapd libqrencode-dev libpng-dev
```
## Installation

6
debian/changelog vendored
View File

@ -1,5 +1,5 @@
linux-wifi-hotspot (4.2.1) UNRELEASED; urgency=low
linux-wifi-hotspot (4.3) UNRELEASED; urgency=low
* Revert Fallback to same channel when same wifi interface does not support for multiple channels
* Add qr code generate support
-- Lakindu Akash <lakinduakash@users.noreply.github.com> Sat, 23 Jan 2021 06:34:00 +0530
-- Lakindu Akash <lakinduakash@users.noreply.github.com> Sat, 18 Oct 2021 04:12:00 +0530

4
debian/control vendored
View File

@ -2,12 +2,12 @@ Source: linux-wifi-hotspot
Section: devel
Priority: optional
Maintainer: Lakindu Akash <lakinduakash@users.noreply.github.com>
Build-Depends: debhelper (>=11~), pkg-config, libgtk-3-dev
Build-Depends: debhelper (>=11~), pkg-config, libgtk-3-dev, libpng-dev, libqrencode-dev
Standards-Version: 4.1.4
Homepage: https://github.com/lakinduakash/linux-wifi-hotspot
Package: linux-wifi-hotspot
Architecture: any
Multi-Arch: foreign
Depends: ${misc:Depends}, ${shlibs:Depends}, hostapd (>=2.0), iw (>= 5.4),iproute2 (>=5.0), util-linux (>=2.3), procps(>=3.3)
Depends: ${misc:Depends}, ${shlibs:Depends}, hostapd (>=2.0), iw (>= 5.4),iproute2 (>=5.0), util-linux (>=2.3), procps(>=3.3), libqrencode4(>=4.0), libpng16-16(>=1.6)
Description: GUI tool for creating virtual hotspots using the same wi-fi card that is already connected to an access point.

View File

@ -2,7 +2,7 @@ CC=gcc
PKGCONFIG = $(shell which pkg-config)
CFLAGS=`pkg-config --cflags gtk+-3.0`
LIBS=`pkg-config --libs gtk+-3.0 --libs x11` -lstdc++
LIBS=`pkg-config --libs gtk+-3.0 --libs x11` -lstdc++ -lpng -lqrencode
APP_NAME="wihotspot"
APP_GUI_BINARY="wihotspot-gui"
@ -16,7 +16,7 @@ GLIB_COMPILE_RESOURCES = $(shell $(PKGCONFIG) --variable=glib_compile_resources
BUILT_SRC = resources.c
_OBJ = main.o ui.o h_prop.o util.o read_config.o about_ui.o qr_ui.o $(BUILT_SRC:.c=.o)
_OBJ = main.o ui.o h_prop.o util.o read_config.o about_ui.o qr_ui.o qrgen.o $(BUILT_SRC:.c=.o)
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))
# Determine this makefile's path.

View File

@ -35,6 +35,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "h_prop.h"
#include "read_config.h"
#include "qrgen.h"
#define BUFSIZE 2048
@ -431,28 +432,32 @@ char* generate_qr_image(char* ssid,char* type,char *password){
qr_image_path = "/tmp/wihotspot_qr.png";
snprintf(cmd, BUFSIZE, "%s -V -s 10 -d 256 -o %s 'WIFI:S:%s;T:%s;P:%s;;' ","qrencode",qr_image_path, ssid,type,password);
// snprintf(cmd, BUFSIZE, "%s -s 10 -d 256 -o %s 'WIFI:S:%s;T:%s;P:%s;;' ","qrencode",qr_image_path, ssid,type,password);
FILE *fp;
// FILE *fp;
char temp_buff[1048];
// char temp_buff[1048];
if ((fp = popen(cmd, "r")) == NULL) {
printf("Error opening pipe!\n");
// if ((fp = popen(cmd, "r")) == NULL) {
// printf("Error opening pipe!\n");
}
// }
while (fgets(temp_buff, sizeof(temp_buff), fp) != NULL) {
// while (fgets(temp_buff, sizeof(temp_buff), fp) != NULL) {
printf("%s", temp_buff);
}
// printf("%s", temp_buff);
// }
if (pclose(fp)) {
printf("Error executing qrencode\n");
// if (pclose(fp)) {
// printf("Error executing qrencode\n");
}
// }
snprintf(cmd, BUFSIZE, "WIFI:S:%s;T:%s;P:%s;;",ssid,type,password);
qr_to_png(cmd,qr_image_path);
return qr_image_path;
}

206
src/ui/qrgen.cpp Normal file
View File

@ -0,0 +1,206 @@
//
// Created by lakinduakash on 18/10/21.
//
/*
Copyright (c) 2021, lakinduaksh
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <qrencode.h>
#include <errno.h>
#include <stdlib.h>
#include <fstream>
#include <string>
#include <iostream>
#include <numeric>
#include <png.h>
#include <cstring>
#include "qrgen.h"
using namespace std;
extern "C"{
static int casesensitive = 0;
static int eightbit = 0;
static int version = 0;
static int size = 8;
static int margin = 5;
static int dpi = 128;
static int structured = 0;
static int rle = 0;
static int micro = 0;
static QRecLevel level = QR_ECLEVEL_L;
static QRencodeMode hint = QR_MODE_8;
static unsigned int fg_color[4] = {0, 0, 0, 255};
static unsigned int bg_color[4] = {255, 255, 255, 255};
// int main()
// {
// const char * line = "The stuff you want to encode";
// QRcode *myqrcode;
// myqrcode = QRcode_encodeString(line, 4, QR_ECLEVEL_H, QR_MODE_8,1);
// writePNG(myqrcode,"filename.png");
// QRcode_free(myqrcode);
// return 0;
// }
void qr_to_png(const char *qrstring,const char *outfile){
QRcode *myqrcode;
myqrcode = QRcode_encodeString(qrstring, 4, QR_ECLEVEL_H, QR_MODE_8,1);
writePNG(myqrcode,outfile);
QRcode_free(myqrcode);
}
static int writePNG(QRcode *qrcode, const char *outfile)
{
static FILE *fp; // avoid clobbering by setjmp.
png_structp png_ptr;
png_infop info_ptr;
png_colorp palette;
png_byte alpha_values[2];
unsigned char *row, *p, *q;
int x, y, xx, yy, bit;
int realwidth;
realwidth = (qrcode->width + margin * 2) * size;
row = (unsigned char *)malloc((realwidth + 7) / 8);
if(row == NULL) {
fprintf(stderr, "Failed to allocate memory.\n");
exit(EXIT_FAILURE);
}
if(outfile[0] == '-' && outfile[1] == '\0') {
fp = stdout;
} else {
fp = fopen(outfile, "wb");
if(fp == NULL) {
fprintf(stderr, "Failed to create file: %s\n", outfile);
perror(NULL);
exit(EXIT_FAILURE);
}
}
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if(png_ptr == NULL) {
fprintf(stderr, "Failed to initialize PNG writer.\n");
exit(EXIT_FAILURE);
}
info_ptr = png_create_info_struct(png_ptr);
if(info_ptr == NULL) {
fprintf(stderr, "Failed to initialize PNG write.\n");
exit(EXIT_FAILURE);
}
if(setjmp(png_jmpbuf(png_ptr))) {
png_destroy_write_struct(&png_ptr, &info_ptr);
fprintf(stderr, "Failed to write PNG image.\n");
exit(EXIT_FAILURE);
}
palette = (png_colorp) malloc(sizeof(png_color) * 2);
if(palette == NULL) {
fprintf(stderr, "Failed to allocate memory.\n");
exit(EXIT_FAILURE);
}
palette[0].red = fg_color[0];
palette[0].green = fg_color[1];
palette[0].blue = fg_color[2];
palette[1].red = bg_color[0];
palette[1].green = bg_color[1];
palette[1].blue = bg_color[2];
alpha_values[0] = fg_color[3];
alpha_values[1] = bg_color[3];
png_set_PLTE(png_ptr, info_ptr, palette, 2);
png_set_tRNS(png_ptr, info_ptr, alpha_values, 2, NULL);
png_init_io(png_ptr, fp);
png_set_IHDR(png_ptr, info_ptr,
realwidth, realwidth,
1,
PNG_COLOR_TYPE_PALETTE,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
png_set_pHYs(png_ptr, info_ptr,
dpi * INCHES_PER_METER,
dpi * INCHES_PER_METER,
PNG_RESOLUTION_METER);
png_write_info(png_ptr, info_ptr);
/* top margin */
memset(row, 0xff, (realwidth + 7) / 8);
for(y=0; y<margin * size; y++) {
png_write_row(png_ptr, row);
}
/* data */
p = qrcode->data;
for(y=0; y<qrcode->width; y++) {
bit = 7;
memset(row, 0xff, (realwidth + 7) / 8);
q = row;
q += margin * size / 8;
bit = 7 - (margin * size % 8);
for(x=0; x<qrcode->width; x++) {
for(xx=0; xx<size; xx++) {
*q ^= (*p & 1) << bit;
bit--;
if(bit < 0) {
q++;
bit = 7;
}
}
p++;
}
for(yy=0; yy<size; yy++) {
png_write_row(png_ptr, row);
}
}
/* bottom margin */
memset(row, 0xff, (realwidth + 7) / 8);
for(y=0; y<margin * size; y++) {
png_write_row(png_ptr, row);
}
png_write_end(png_ptr, info_ptr);
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
free(row);
free(palette);
return 0;
}
}

52
src/ui/qrgen.h Normal file
View File

@ -0,0 +1,52 @@
//
// Created by lakinduakash on 18/10/21.
//
/*
Copyright (c) 2021, lakinduaksh
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef WIHOTSPOT_QRGEN_H
#define WIHOTSPOT_QRGEN_H
#include <qrencode.h>
#ifdef __cplusplus
extern "C" {
#endif
#define INCHES_PER_METER (100.0/2.54)
static int writePNG(QRcode *qrcode, const char *outfile);
void qr_to_png(const char *qrstring,const char *outfile);
#ifdef __cplusplus
}
#endif
#endif //WIHOTSPOT_QRGEN_H

View File

@ -166,7 +166,6 @@ static void on_about_open_click(GtkWidget *widget, gpointer data){
static void on_qr_open_click(GtkWidget *widget, gpointer data){
char* image_path = generate_qr_image(configValues.ssid,"WPA",configValues.pass);
open_qr(widget,data,image_path);
}
@ -461,7 +460,9 @@ void init_ui_from_config(){
if(read_config_file()==READ_CONFIG_FILE_SUCCESS){
ConfigValues *values=getConfigValues();
configValues=*getConfigValues();
ConfigValues *values=&configValues;
//TODO do properly
configValues.accepted_mac_file=values->accepted_mac_file;
@ -607,6 +608,7 @@ void lock_running_views(gboolean set_lock){
gtk_widget_set_sensitive ((GtkWidget*)button_create_hp, FALSE);
gtk_widget_set_sensitive ((GtkWidget*)button_stop_hp, TRUE);
gtk_widget_set_sensitive ((GtkWidget*)button_qr, TRUE);
gtk_widget_set_sensitive ((GtkWidget*)combo_internet, FALSE);
gtk_widget_set_sensitive ((GtkWidget*)combo_wifi, FALSE);
@ -618,6 +620,7 @@ void lock_running_views(gboolean set_lock){
gtk_widget_set_sensitive ((GtkWidget*)button_create_hp, TRUE);
gtk_widget_set_sensitive ((GtkWidget*)button_stop_hp, FALSE);
gtk_widget_set_sensitive ((GtkWidget*)button_qr, FALSE);
gtk_widget_set_sensitive ((GtkWidget*)combo_internet, TRUE);
gtk_widget_set_sensitive ((GtkWidget*)combo_wifi, TRUE);